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

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