I'm new to QT5, and I'm having a very strange problem.
I want to use QTimer to read coordinate from serial device every 0.5 second, but the QTimer won't send timeout signal.
My .h:
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void test();
private:
Ui::MainWindow *ui;
};
My .cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer myTimer(this);
myTimer.setInterval(500);
myTimer.setSingleShot(false);
connect(&myTimer, SIGNAL(timeout()), this, SLOT(test()));
myTimer.start();
}
void MainWindow::test() {
qDebug() << "Time out";
}
MainWindow::~MainWindow()
{
delete ui;
}
After I started the program, not a single "Time out" shows up...
I added the following codes after "myTimer.start()":
QTime t = QTime::currentTime().addMSecs(550);
while (QTime::currentTime() < t) {
qDebug() << myTimer.remainingTime();
}
And I found that after the remaining time of "myTimer" reduced to 0, it won't refill the remaining time and start once again, it remains at 0.
Q_OBJECT is already added
Anyone got an idea?
Thank you very much!
The problem is here:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer myTimer(this);
myTimer.setInterval(500);
myTimer.setSingleShot(false);
connect(&myTimer, SIGNAL(timeout()), this, SLOT(test()));
myTimer.start();
}
In the constructor you declared myTimer automatically, so it will be destroyed as soon as constructor returns. This way, by the time when you expect timeout event, myTimer does not exist anymore and thus it cannot send any signals!
The solution is straightforward: myTimer should exist all the time MainWindow object lives, so declare it in the class, not in it's constructor, and allocate it dynamically:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void test();
private:
QTimer *myTimer; // <--- pointer declaration.
Ui::MainWindow *ui;
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Allocating actual QTimer object:
myTimer = new QTimer(this);
// Calling methods by object pointer:
myTimer->setInterval(500);
myTimer->setSingleShot(false);
connect(myTimer, SIGNAL(timeout()), this, SLOT(test()));
myTimer->start();
}
MainWindow::~MainWindow()
{
// Don't forget to delete after usage.
delete myTimer;
delete ui;
}
myTimer is local.
Make it a class member and use it. It will work.
Related
How can I run loop functions in different QThreads? I need to run in different threads because if I don't do it, the loops break.
How to make the on_pushTurnOn_clicked be executed in other thread, and the loop that pushTurnOn must be able to be canceled by on_pushTurnOff_clicked even on a different thread.
MainWindow .h code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QThread> //I don't know how to use it
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushTurnOn_clicked();
void on_pushTurnOff_clicked();
private:
QTimer *timerLoop;
};
#endif // MAINWINDOW_H
MainWindow cpp code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushTurnOn_clicked()
{
timerLoop = new QTimer(this);
timerLoop->setInterval(3000);
timerLoop->setSingleShot(true);
connect(timerLoop, SIGNAL(timeout()),
SLOT(on_pushTurnOn_clicked()));
qDebug()<<"the loop was started";
timerLoop->start(); //start the loop
}
void MainWindow::on_pushTurnOff_clicked()
{
qDebug()<<"the loop was stoped";
timerLoop->stop(); //stop the loop
}
I will start with a side note. Your memory allocation for timerLoop in on_pushTurnOn_clicked() will cause a memory leak due to absence of deallocation, even though it is a single shot timer. You are creating a new QTimer without destroying the previous allocated one. The better approach is to just allocate memory once in the constructor and just start the timer in on_pushTurnOn_clicked().
Coming back to your question, you will have to split your class into two, one is your mainWindow which has the slots for on_pushTurnOn_clicked() and on_pushTurnOff_clicked(), other possible UI elements and non-intensive tasks.
The second class is a worker which contains the QTimer along with the actual work load. So you would essentially have something like this:
MainWindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//constructor
ui->setupUi(this);
this->worker = new Worker(); //object of WorkerClass type
QThread *thread = new QThread();
worker->moveToThread(thread);
connect(this, SIGNAL(startOrStopTimerSignal(bool), worker, SLOT(startStopTimer(bool));
}
void MainWindow::on_pushTurnOn_clicked()
{
if(Busy==1){
qDebug()<<"Is busy!";
}
if(Busy==0){
FuncaoLoop(RandomParameter1, RandomParameter2);
}
//start the timer through a signal (timers need to be started in their own threads)
emit startOrStopTimerSignal(true);
}
void MainWindow::on_pushTurnOff_clicked()
{
emit startOrStopTimerSignal(false); //stop the timer
}
WorkerClass.h/.cpp:
WorkerClass::WorkerClass(QObject *parent) : QObject(parent)
{
timerLoop= new QTimer(this);
connect(timerLoop, SIGNAL(timeout()), this, SLOT(workLoad())); //timer connect to your actual task
}
void WorkerClass::startStopTimer(bool start)
{
if (start)
timerLoop->start();
else
timerLoop->stop();
}
WorkerClass::workLoad()
{
//whatever task for your PLC
}
Hope this helps you out.
In order to shrink the problem I created a minimal verifiable example below.
I am trying to implement a 60 minutes count down using a QLabel.
The problem I have is that instead of seeing the time going back, I see the the current time going forward. And no countdown goes back.
Below the minimal verifiable example:
mainwindow.h
#include <QMainWindow>
#include <QTime>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void timerUpdate();
private:
Ui::MainWindow *ui;
QTimer *timer;
QTime time;
};
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ui->countDown->setText(time.currentTime().toString("hh:mm:ss"));
ui->countDown->setText("60:00");
//ui->countDown->setText(time.toString("hh:mm:ss"));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerUpdate()
{
time = time.addSecs(-1);
ui->countDown->setText(time.currentTime().toString("hh:mm:ss"));
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
EDIT 2
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//ui->countDown->setText(time.toString("hh:mm:ss"));
//ui->countDown->setText("60:00");
ui->countDown->setText(time.toString("hh:mm:ss"));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerUpdate()
{
time = time.addSecs(-1);
ui->countDown->setText(time.toString("hh:mm:ss"));
}
I followed exactly the official documentation but despite that it still does not work. In addition to that I consulted also the QTime class to make sure I was respecting the members function to call.
I tried also another thign which is setting the constructor to setHMS and providing the proper values related to 1 hour, but that also didn't work.
I dug more and came across this post which uses a very similar approach to what I used, with the difference that the example implements a current timer and not a count down. That is the reason why in the timerUpdate() function I am diminishing time time = time.addSecs(-1); instead of adding it. But still does not work.
Thanks for pointing in the right direction to solve this problem.
What I'm trying to do is to call an time consuming operation (MockClamWrapper::loadDatabase()) in a separate thread at the moment of creation of my window and then to update my window once the operation is completed. Here is the code that I have.
MockClamWrapper.h
class MockClamWrapper : QObject
{
Q_OBJECT
public:
MockClamWrapper();
~MockClamWrapper();
bool loadDatabase(unsigned int *signatureCount=NULL);
Q_SIGNALS:
void databaseLoaded();
};
MockClamWrapper.cpp
bool MockClamWrapper::loadDatabase(unsigned int *signatureCount){
QThread::currentThread()->sleep(10);
databaseLoaded();
return true;
}
MainWindow.h
#include <QMainWindow>
#include <QFileDialog>
#include "mockclamwrapper.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public slots:
void enableWindow();
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
MockClamWrapper *clam;
void initWindow();
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect((QObject*)clam, SIGNAL(databaseLoaded()),(QObject*)this,SLOT(enableWindow()));
QFuture<void> fut = QtConcurrent::run(this,&MainWindow::initWindow);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initWindow(){
clam->loadDatabase(NULL);
}
void MainWindow::enableWindow(){
ui->checkFileButton->setEnabled(true);
}
The program compiles, but it crashes right after start. I assume that I do something wrong with slots and signals, but can't find my mistake.
The reason for crash is that you are not making any instance of the class MockClamWrapper. In the connect statement, you are referencing a pointer that points to nothing. Make a new object and then connect :
clam = new MockClamWrapper();
connect(clam, SIGNAL(databaseLoaded()), this, SLOT(enableWindow()));
I made a simple Qt project to cover the issue of calling Ui from another class.
The Files:
mainwindow.h | mainwindow.cpp | client.h | client.cpp | main.cpp
The Issue:
Connecting a signal from client.cpp to a slot in mainwindow.cpp worked very well.
But when I added a ui->statusBar->showMessage("message");
in the slot, it didn't work.
NOTE: When I made the signal and the slot both in mainwindow.cpp it worked, but calling a slot from mainwindow.cpp from a signal and connect() in client.cpp doesn't work.
The Codes: (trimmed to the essentials)
mainwindow.h
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
signals:
public slots:
void doSomething();
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
void MainWindow::doSomething()
{
QMessageBox::information(0, "test", "BINGO!!");
ui->statusBar->showMessage("testing");
}
client.h
class client : public QObject
{
Q_OBJECT
public:
explicit client(QObject *parent = 0);
void call();
signals:
void connected();
public slots:
};
client.cpp
client::client(QObject *parent) :
QObject(parent)
{
MainWindow main;
connect(this, SIGNAL(connected()), &main, SLOT(doSomething()));
call();
}
void client::call()
{
emit connected();
}
Added:
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Thanks.
client::client(QObject *parent) :
QObject(parent)
{
MainWindow main;
connect(this, SIGNAL(connected()), &main, SLOT(doSomething()));
call();
}
Your MainWindow lives on the stack. I think by the time your doSomething slot is triggered, the MainWindow object is already long gone. Try creating your MainWindow on the heap instead.
I see a major mistake in the code you have posted.
You have created an instance of MainWindow in the file main.cpp and thats the window you see when you run the application. But in your client.cpp you are creating a second instance of MainWindow and connecting the signal from the client object to the slot of the second MainWindow object you create. You are not seeing two main windows because you do not run the exec or show methods of this second instance.
Now coming to the solution, you will have to connect the signal from client to the proper MainWindow instance. I will suggest two methods below.
First:
I believe you create an instance of client in one of the member functions of the class MainWindow. If so then shift the connect method to the file mainwindow.cpp
Something like:
void MainWindow::someFunction()
{
client* c = new client();
connect(c, SIGNAL(connected()), this, SLOT(doSomething()));
c->call();
}
Second:
If you cannot do what is explained above, then you can use a combination of a static pointer and a static function to get the instance of MainWindow. Note that this method can also be used to ensure that there is only one instance of MainWindow at any given time.
mainwindow.h
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
signals:
public slots:
void doSomething();
public:
explicit MainWindow(QWidget* parent = 0);
~MainWindow();
static MainWindow* GetInstance(QWidget* parent = 0);
private:
Ui::MainWindow *ui;
static MainWindow* mainInstance;
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow* MainWindow::mainInstance = 0;
MainWindow* MainWindow::GetInstance(QWidget *parent)
{
if(mainInstance == NULL)
{
mainInstance = new MainWindow(parent);
}
return mainInstance;
}
void MainWindow::doSomething()
{
QMessageBox::information(0, "test", "BINGO!!");
ui->statusBar->showMessage("testing");
}
Now in whatever class you need to get the instance of MainWindow in use, simply use the static function MainWindow::GetInstance to get the pointer to the MainWindow instance and use that as the parameter for connect.
i.e.,
MainWindow* instance = MainWindow::GetInstance();
client* c = new client();
connect(c, SIGNAL(connected()), instance , SLOT(doSomething()));
Edit: I removed the destructor from the slot. But now I have memory leaking problems. Each new window that I open occupies some memory,and when I close it,the memory stays occupied
When I execute the program,and open new windows, they are opened normally. When I close any of them, the whole application crashes (not only that specific window),and I get the crash error.
What am I doing wrong?
mainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QHBoxLayout;
class QTextEdit;
class QWidget;
class QDialog;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void closeWindow();
void newWindow();
private:
Ui::MainWindow *ui;
MainWindow *tempMainWindow;
QHBoxLayout * mainLyt;
QTextEdit *txtEdit;
QWidget *mainWidget;
};
#endif // MAINWINDOW_H
mainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QHBoxLayout>
#include <QTextEdit>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mainWidget=new QWidget();
mainLyt=new QHBoxLayout();
txtEdit=new QTextEdit();
mainLyt->addWidget(txtEdit);
mainWidget->setLayout(mainLyt);
setCentralWidget(mainWidget);
connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(closeWindow()));
connect(ui->actionNew,SIGNAL(triggered()),this,SLOT(newWindow()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeWindow()
{
this->close();
delete txtEdit;
delete mainLyt;
delete mainWidget;
this->~MainWindow();
}
void MainWindow::newWindow()
{
tempMainWindow=new MainWindow(this);
tempMainWindow->show();
}
If you pass to QWidget(), QHBoxLayout() and QTextEdit() also this (which is the parent), at the delection of the MainWindow Qt will delete for you the ui and all the additional widgets yur defined in the construstor. In this way you can avoid to call closeWindow() method.
delete ui is also not necessary.
ui->setupUi(this);
mainWidget = new QWidget(this);
mainLyt = new QHBoxLayout(this);
txtEdit = new QTextEdit(this);
I'm trying to make basic text editor,and when New is triggered, it should open a new window for new text document. Is there some better way to do this?
Yes. It's called a factory, and it can be a static method as it doesn't operate on any object. You can call it from a slot, of course.
I imagine you'll need to pass a file name to the newly created window - that could be an argument to the factory method and the factory slot. If the "new" window is empty, then this is not an issue.
Other issues:
There is no reason to keep the mainWidget member: it is always available as centralWidget().
There's also no reason to have the members other than ui as pointers. It is actually a premature pessimization - it will waste a bit more heap memory.
You don't need a layout for the central widget if it has no child widgets. The QTextEdit instance itself can be the central widget.
The ui instance should be retained using a smart pointer. This makes the destructor completely compiler-generated (it has an empty body).
You don't need anything fancy in the closeWindow slot. Simply delete the instance!
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
static MainWindow * createWindow();
void setFileName(const QString & fileName);
public slots:
void closeWindow();
void newWindow();
private:
QScopedPointer<Ui::MainWindow> const ui;
QTextEdit m_txtEdit;
};
void MainWindow::newWindow() {
createWindow()->show();
}
void MainWindow::closeWindow() {
deleteLater();
}
MainWindow * MainWindow::createWindow(QWidget * parent) {
return new MainWindow(parent);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setCentralWidget(&m_txtEdit);
connect(ui->actionExit, SIGNAL(triggered()), SLOT(closeWindow()));
connect(ui->actionNew, SIGNAL(triggered()), SLOT(newWindow()));
}
MainWindow::~MainWindow()
{}
Let us see your code:
this->close();
delete txtEdit;
delete mainLyt;
delete mainWidget;
this->~MainWindow();
You are trying to destroy them and for the next open you allocate them almost the same way.
What you achieve here is basically performance penalty. I would suggest to hide, modify, and so on the unwanted items instead.