QTimer: Getting Started problems
-
Slot connection is probably wrong
No need to reimplement the paint event
This should work:
@
mita_screen::mita_screen(QWidget *parent) :
QDialog(parent),
ui(new Ui::mita_screen)
{
mita_screen::setWindowFlags(Qt::SplashScreen);
ui->setupUi(this);mita_state = 0; // Initialise the state machine QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(state_update())); timer->start(1000);
}
void mita_screen::state_update()
{
mita_state++;
ui->label->setText(QString::number(mita_state));}
@ -
Wow, we really flooded the poor guy :)
-
[quote author="sierdzio" date="1366112776"]Wow, we really flooded the poor guy :)[/quote]
yep...within 3 mins ... lol
-
[quote author="sierdzio" date="1366112776"]Wow, we really flooded the poor guy :)[/quote]
Like one other user said, we're parallelizing :D
-
Yeah, but it would be a tad better to spread across different threads (pun well intended :)).
-
SGaist said:-
bq. Why do you delete your ui variable in on_pushButton_clicked ? Also note, that it’s already deleted in the destructor, so calling that function, then the destructor will cause a crash.
If you want to close the dialog, connect the pushButton clicked signal on the close slot of the dialog.I changed my code to connect the on_pushButton_clicked() signal to the ~mita_screen() slot but the button has no effect when pressed. See below.
Can anyone please tell me whats wrong here?
@#include "mita_screen.h"
#include "ui_mita_screen.h"mita_screen::mita_screen(QWidget *parent) :
QDialog(parent),
ui(new Ui::mita_screen)
{
mita_screen::setWindowFlags(Qt::SplashScreen);
ui->setupUi(this);mita_state = 0; // Initialise the state machine QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(state_update())); timer->start(1000); connect(ui->pushButton, SIGNAL(on_pushButton_clicked()), this, SLOT(~mita_screen()));
}
mita_screen::~mita_screen()
{
delete ui;
}void mita_screen::state_update()
{
ui->label->setText(QString("%1").arg(++mita_state));}
void mita_screen::on_pushButton_clicked()
{}
void mita_screen::on_pushButton_pressed()
{}
@ -
You're connecting to the destructor:
1.It's not a slot
2.Never call a destructor, it's done for you when the object is destroyed.As I said, connect to the "close slot":http://qt-project.org/doc/qt-4.8/qwidget.html#close
-
you can't connect to a destructor. Since your widget is a dialog connect it to the close() slot and additionaly to the deleteLater() slot or set the "widget attribute":http://qt-project.org/doc/qt-4.8/qwidget.html#setAttribute: to Qt::WA_DeleteOnClose to let Qt delete the dialog for you.
-
Sorry, that was an older version, somehow. Thanks for your patience!
With this code, the button is ignored and has no effect.What I meant to post was:-
@#include "mita_screen.h"
#include "ui_mita_screen.h"
#include <QtGui>
#include <QLabel>mita_screen::mita_screen(QWidget *parent) :
QDialog(parent),
ui(new Ui::mita_screen)
{
mita_screen::setWindowFlags(Qt::SplashScreen);
ui->setupUi(this);mita_state = 0; // Initialise the state machine QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(state_update())); timer->start(1000); connect(ui->pushButton, SIGNAL(on_pushButton_clicked()), this, SLOT(mita_screen::close() )); connect(ui->pushButton, SIGNAL(on_pushButton_clicked()), this, SLOT(mita_screen::deleteLater() ));
}
mita_screen::~mita_screen()
{
delete ui;
}void mita_screen::state_update()
{
static int counter=0;ui->label->setText(QString("%1").arg(++mita_state));
}
void mita_screen::on_pushButton_clicked()
{}
void mita_screen::on_pushButton_pressed()
{}
@ -
In your connect statement:
Not:
@
SLOT(mita_screen::close())
@Instead:
@
SLOT(close())
@ -
on_pushButton_clicked is a slot.
Try this:
@
connect(ui->pushButton, SIGNAL(clicked()), SLOT(close()));
@Have a look at the "signal/slot":http://qt-project.org/doc/qt-4.8/signalsandslots.html and the "designer":http://qt-project.org/doc/qt-4.8/designer-manual.html documentation to have a better understanding of how it works