Currently in a situation where I want to have two classes, one a layout class controlling all the GUI items that are being displayed. Then a second class that contains implementation methods, what I would like to specifically do is press a button that would add 10 to a count number, then use this count number to update a progress bar (just a basic example so i get my head round it).
So currently looking at implementing this I decided to use Qt5 version of signals and slots, below is my current attempt which seems not to be firing off the slot even though i'm not receiving a compiler or runtime error. If anyone has an idea how I should be implementing this that would be great, or a good link to a decent example would be great.
counter.h
#ifndef COUNTER_H
#define COUNTER_H
#include <QObject>
{
Q_OBJECT
public:
explicit counter
(QObject *parent
= 0);
signals:
public slots:
int count();
private:
static int number;
};
#endif // COUNTER_H
#ifndef COUNTER_H
#define COUNTER_H
#include <QObject>
class counter : public QObject
{
Q_OBJECT
public:
explicit counter(QObject *parent = 0);
signals:
public slots:
int count();
private:
static int number;
};
#endif // COUNTER_H
To copy to clipboard, switch view to plain text mode
counter.cpp
#include "counter.h"
#include "mainwindow.h"
int counter::number;
counter
::counter(QObject *parent
) :{
number = 10;
}
int counter::count()
{
number += 10;
//emit countworked;
qDebug() << number;
return number;
}
#include "counter.h"
#include "mainwindow.h"
int counter::number;
counter::counter(QObject *parent) :
QObject(parent)
{
number = 10;
}
int counter::count()
{
number += 10;
//emit countworked;
qDebug() << number;
return number;
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QWidget>
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QState>
#include <QStateMachine>
#include <QDebug>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QWidget>
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QState>
#include <QStateMachine>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "counter.h"
#include <QDebug>
#include <iostream>
#include <string>
#include <sstream>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
counter counting;
this
->setWindowTitle
(QApplication::translate("toplevel",
"CCTV"));
//int (counter::*intfunction)();
//int count(10);
//intfunction = &counter::count;
connect(ui->pushButton_2,&QPushButton::clicked,&counting,&counter::count);
//connect(countworked,countworkedvaluechanged(),ui->progressBar,&QProgressBar::valueChanged)
// int *valuechanged = *intfunction;
ui->progressBar->setValue(50);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "counter.h"
#include <QDebug>
#include <iostream>
#include <string>
#include <sstream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
counter counting;
this->setWindowTitle(QApplication::translate("toplevel", "CCTV"));
//int (counter::*intfunction)();
//int count(10);
//intfunction = &counter::count;
connect(ui->pushButton_2,&QPushButton::clicked,&counting,&counter::count);
//connect(countworked,countworkedvaluechanged(),ui->progressBar,&QProgressBar::valueChanged)
// int *valuechanged = *intfunction;
ui->progressBar->setValue(50);
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks