slider checkpoint

This commit is contained in:
2025-12-21 18:31:06 -06:00
parent dcd9886df2
commit 9d6de850d9
16 changed files with 537 additions and 245 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
build/* build/*
.vscode/*

View File

@@ -1,44 +1,21 @@
cmake_minimum_required(VERSION 3.22) cmake_minimum_required(VERSION 3.21)
project(metabolus LANGUAGES CXX)
project(metabolus
VERSION 0.1
LANGUAGES CXX
)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_PREFIX_PATH "C:/Qt/6.10.1/mingw_64") find_package(Qt6 REQUIRED COMPONENTS Widgets)
find_package(Qt6 6.10 REQUIRED COMPONENTS
Core
Gui
Qml
Quick
)
qt_standard_project_setup() qt_standard_project_setup()
qt_add_executable(metabolus qt_add_executable(metabolus
src/main.cpp src/main.cpp
src/AppController.cpp src/MainWindow.cpp
src/AppController.h src/MainWindow.h
) src/MainWindow.ui
resources/resources.qrc
qt_add_qml_module(metabolus
URI MyApp
VERSION 1.0
QML_FILES
qml/Main.qml
qml/screens/MainScreen.qml
qml/screens/MainScreenUI.ui.qml
) )
target_link_libraries(metabolus target_link_libraries(metabolus
PRIVATE PRIVATE Qt6::Widgets
Qt6::Core
Qt6::Gui
Qt6::Qml
Qt6::Quick
) )

View File

@@ -1,15 +0,0 @@
import QtQuick
import QtQuick.Window
import MyApp 1.0
Window {
width: 800
height: 600
visible: true
title: "metabolus"
color: "black"
MainScreen {
anchors.fill: parent
}
}

View File

@@ -1 +0,0 @@
module MyApp

View File

@@ -1,9 +0,0 @@
import QtQuick
import QtQuick.Controls
import MyApp 1.0
MainScreenUI {
//onIncrementClicked: app.increment()
//onResetClicked: app.reset()
}

View File

@@ -1,99 +0,0 @@
/*
This is a UI file (.ui.qml) that is intended to be edited in Qt Design Studio only.
It is supposed to be strictly declarative and only uses a subset of QML. If you edit
this file manually, you might introduce QML code that is not supported by Qt Design Studio.
Check out https://doc.qt.io/qtcreator/creator-quick-ui-forms.html for details on .ui.qml files.
*/
import QtQuick
import QtQuick.Controls
import MyApp 1.0
Rectangle {
id: rectangle
width: Constants.width
height: Constants.height
color: "#181818"
Slider {
id: slider
x: 300
y: 282
value: 0.5
rotation: -90
live: true
}
TextInput {
id: sliderMax
x: 360
y: 179
width: 80
height: 20
color: "#b9eaff"
text: qsTr("100")
font.pixelSize: 20
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignBottom
}
TextInput {
id: sliderMin
x: 360
y: 401
width: 80
height: 20
color: "#b9eaff"
text: qsTr("100")
font.pixelSize: 20
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignBottom
}
TextInput {
id: sliderValue
x: 360
y: 153
width: 80
height: 20
color: "#63b9de"
text: qsTr("100")
font.pixelSize: 30
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignBottom
}
Button {
id: buttonReset
x: 359
y: 504
width: 81
height: 32
text: qsTr("Reset")
}
Button {
id: buttonIncrement
x: 359
y: 542
text: qsTr("Increment")
}
Text {
id: text1
x: 340
y: 471
width: 120
height: 27
color: "#e8cece"
text: qsTr("Text")
font.pixelSize: 20
horizontalAlignment: Text.AlignHCenter
}
states: [
State {
name: "clicked"
}
]
}

0
resources/resources.qrc Normal file
View File

View File

@@ -1,11 +0,0 @@
@echo off
rmdir /S /Q build
mkdir build
cd build
cmake .. -G "MinGW Makefiles"
cmake --build .
C:\Qt\6.10.1\mingw_64\bin\windeployqt.exe metabolus.exe

3
scripts/build.sh Executable file
View File

@@ -0,0 +1,3 @@
cmake -S . -B build -G Ninja
cmake --build build

View File

@@ -1,29 +0,0 @@
#include "AppController.h"
AppController::AppController(QObject *parent)
: QObject(parent)
{
m_status = "Ready";
}
void AppController::setSliderValue(int val) {
if(sliderValue_ == val) return;
sliderValue_ = val;
emit sliderValueChanged();
}
void AppController::setSliderMin(int val) {
if(sliderMin_ == val) return;
sliderMin_ = val;
emit sliderMinChanged();
}
void AppController::setSliderMax(int val) {
if(sliderMax_ == val) return;
sliderMax_ = val;
emit sliderMaxChanged();
}

View File

@@ -1,30 +0,0 @@
#pragma once
#include <QObject>
// AppController is the bridge from the QML logic to the C++ app
class AppController : public QObject
{
Q_OBJECT
Q_PROPERTY(int sliderValue READ sliderValue WRITE setSliderValue NOTIFY sliderValueChanged)
Q_PROPERTY(int sliderMin READ sliderMin WRITE setSliderMin NOTIFY sliderMinChanged)
Q_PROPERTY(int sliderMax READ sliderMax WRITE setSliderMax NOTIFY sliderMaxChanged)
public:
explicit AppController(QObject *parent = nullptr);
int sliderValue(int val) { return sliderValue_; }
int sliderMin(int val) { return sliderMin_; }
int sliderMax(int val) { return sliderMax_; }
void setSliderValue();
void setSliderMin();
void setSliderMax();
private:
int sliderValue_;
int sliderMin_;
int sliderMax_;
};

88
src/MainWindow.cpp Normal file
View File

@@ -0,0 +1,88 @@
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Initialize UI
updateCounterLabel();
// Connect buttons to slots
connect(ui->buttonIncrement, &QPushButton::clicked, this, &MainWindow::onIncrementClicked);
connect(ui->buttonReset, &QPushButton::clicked, this, &MainWindow::onResetClicked);
// slider business
connect(ui->slider, &QSlider::valueChanged, this, &MainWindow::onSliderValueChanged);
connect(ui->inputMin, &QLineEdit::editingFinished, this, &MainWindow::onMinChanged);
connect(ui->inputMax, &QLineEdit::editingFinished, this, &MainWindow::onMaxChanged);
connect(ui->inputStep, &QLineEdit::editingFinished, this, &MainWindow::onStepChanged);
connect(ui->inputValue, &QLineEdit::editingFinished, this, &MainWindow::onValueChanged);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::onIncrementClicked() {
counter_++;
updateCounterLabel();
}
void MainWindow::onResetClicked() {
counter_ = 0;
updateCounterLabel();
}
void MainWindow::updateCounterLabel() {
ui->labelCounter->setText(QString::number(counter_));
}
// allows only numbers to be set
void MainWindow::onSliderValueChanged(int value) {
QSignalBlocker blocker(ui->inputValue);
ui->inputValue->setText(QString::number(value));
}
// allows only values within the min, max to be set by the text field
void MainWindow::onValueChanged() {
bool ok = false;
int value = ui->inputValue->text().toInt(&ok);
if(!ok) return;
value = qBound(ui->slider->minimum(), value, ui->slider->maximum());
ui->slider->setValue(value);
}
void MainWindow::applySliderRange() {
bool minOk, maxOk;
int min = ui->inputMin->text().toInt(&minOk);
int max = ui->inputMax->text().toInt(&maxOk);
if(!minOk || !maxOk || min > max) return;
ui->slider->setRange(min, max);
syncValueToUi(ui->slider->value());
}
void MainWindow::applySliderStep() {
bool ok;
int step = ui->inputStep->text().toInt(&ok);
if(!ok || step <= 0) return;
ui->slider->setSingleStep(step);
ui->slider->setPageStep(step);
}
void MainWindow::syncValueToUi(int value) {
QSignalBlocker block1(ui->slider);
QSignalBlocker block2(ui->inputValue);
value = qBound(ui->slider->minimum(), value, ui->slider->maximum());
ui->slider->setValue(value);
ui->inputValue->setText(QString::number(value));
}

41
src/MainWindow.h Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void onIncrementClicked();
void onResetClicked();
void onSliderValueChanged(int value);
void onMinChanged() { applySliderRange(); }
void onMaxChanged() { applySliderRange(); }
void onStepChanged() { applySliderStep(); }
void onValueChanged();
private:
Ui::MainWindow *ui;
int counter_ = 0;
int value = 0;
void updateCounterLabel();
void applySliderRange();
void applySliderStep();
void syncValueToUi(int value);
};

194
src/MainWindow.ui Normal file
View File

@@ -0,0 +1,194 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="buttonIncrement">
<property name="geometry">
<rect>
<x>50</x>
<y>70</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>15</pointsize>
</font>
</property>
<property name="text">
<string>Increment</string>
</property>
</widget>
<widget class="QPushButton" name="buttonReset">
<property name="geometry">
<rect>
<x>50</x>
<y>100</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>15</pointsize>
</font>
</property>
<property name="text">
<string>Reset</string>
</property>
</widget>
<widget class="QLabel" name="labelCounter">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>151</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>30</pointsize>
</font>
</property>
<property name="text">
<string>Counter</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
<widget class="QSlider" name="slider">
<property name="geometry">
<rect>
<x>390</x>
<y>210</y>
<width>16</width>
<height>160</height>
</rect>
</property>
<property name="value">
<number>50</number>
</property>
<property name="orientation">
<enum>Qt::Orientation::Vertical</enum>
</property>
</widget>
<widget class="QLineEdit" name="inputMax">
<property name="geometry">
<rect>
<x>370</x>
<y>190</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::LeftToRight</enum>
</property>
<property name="text">
<string>100</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="inputStep">
<property name="geometry">
<rect>
<x>370</x>
<y>170</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::LeftToRight</enum>
</property>
<property name="text">
<string>100</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="inputValue">
<property name="geometry">
<rect>
<x>360</x>
<y>140</y>
<width>81</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::LeftToRight</enum>
</property>
<property name="text">
<string>100</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="inputMin">
<property name="geometry">
<rect>
<x>370</x>
<y>370</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::LeftToRight</enum>
</property>
<property name="text">
<string>100</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -1,24 +1,12 @@
#include <QGuiApplication> #include <QApplication>
#include <QQmlApplicationEngine> #include "MainWindow.h"
#include <QQmlContext>
#include "AppController.h" int main(int argc, char *argv[]) {
QApplication app(argc, argv);
int main(int argc, char *argv[]) MainWindow window;
{ window.show();
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
AppController controller;
engine.rootContext()->setContextProperty("appController", &controller);
engine.loadFromModule("MyApp", "Main");
if (engine.rootObjects().isEmpty()) {
return -1;
}
return app.exec(); return app.exec();
} }

194
untitled.ui Normal file
View File

@@ -0,0 +1,194 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="buttonIncrement">
<property name="geometry">
<rect>
<x>50</x>
<y>70</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>15</pointsize>
</font>
</property>
<property name="text">
<string>Increment</string>
</property>
</widget>
<widget class="QPushButton" name="buttonReset">
<property name="geometry">
<rect>
<x>50</x>
<y>100</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>15</pointsize>
</font>
</property>
<property name="text">
<string>Reset</string>
</property>
</widget>
<widget class="QLabel" name="labelCounter">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>151</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>30</pointsize>
</font>
</property>
<property name="text">
<string>Counter</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
<widget class="QSlider" name="slider">
<property name="geometry">
<rect>
<x>390</x>
<y>210</y>
<width>16</width>
<height>160</height>
</rect>
</property>
<property name="value">
<number>50</number>
</property>
<property name="orientation">
<enum>Qt::Orientation::Vertical</enum>
</property>
</widget>
<widget class="QLineEdit" name="inputMax">
<property name="geometry">
<rect>
<x>370</x>
<y>190</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::LeftToRight</enum>
</property>
<property name="text">
<string>100</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="inputStep">
<property name="geometry">
<rect>
<x>370</x>
<y>170</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::LeftToRight</enum>
</property>
<property name="text">
<string>100</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="inputValue">
<property name="geometry">
<rect>
<x>360</x>
<y>140</y>
<width>81</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::LeftToRight</enum>
</property>
<property name="text">
<string>100</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="inputMin">
<property name="geometry">
<rect>
<x>370</x>
<y>370</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LayoutDirection::LeftToRight</enum>
</property>
<property name="text">
<string>100</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>