Q_EMIT not working from connected slot, only with QTimer slot
-
I am trying to use signals and slots to update a widget of another class. I am trying to emit a signal, which works if the slot is hooked up to a QTimer. However if the slot is hooked up to a QPushButton for example, it doesn't work. For example, I have a run() function that it connected to a 1 second QTimer. The run() function contains "Q_EMIT textChanged("Test");" This signal works as expected, emitting a signal. However, if I have a QPushButton connected to a slot, which also contains "Q_EMIT textChanged("Test");" nothing happens when I press the QPushButton...why is this/how can I fix this???
counter.h
class Counter : public QWidget { Q_OBJECT public: explicit Counter(QWidget *parent = 0); signals: void textChanged(const QString &text); public slots: void run(); void OKalarmLimits();
mainwindow.h
class MainWindow : public QDialog
{
Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); QGridLayout *layout; ~MainWindow(); QPlainTextEdit *box; public slots: void updateWidgets(const QString &text); void run();
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QDialog(parent) { QWidget::setFixedHeight(600); QWidget::setFixedWidth(600); layout = new QGridLayout; box = new QPlainTextEdit(); box->setMaximumHeight(400); box->setMinimumWidth(400); layout->addWidget(box,0,0); QTabWidget *tabWidget = new QTabWidget; tabWidget->addTab(new Counter(), tr("Counter")); layout->addWidget(tabWidget,1,0); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(run())); timer->start(1000); setLayout(layout); } MainWindow::~MainWindow() { } void MainWindow::updateWidgets(const QString &text) { qDebug() << "Here"; box->appendPlainText(text); } void MainWindow::run() { box->appendPlainText("Test"); }
main.cpp
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow s; Counter m; s.show(); QObject::connect(&m, SIGNAL(textChanged(QString)), &s,SLOT(updateWidgets(QString))); return a.exec(); }
counter.cpp
Counter::Counter(QWidget *parent) : QWidget(parent) { QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(run())); timer->start(1000); QGridLayout *layout = new QGridLayout; QPushButton *OK = new QPushButton("OK"); OK->setFixedWidth(50); connect(OK,SIGNAL(clicked()),this,SLOT(OKalarmLimits())); layout->addWidget(OK); setLayout(layout); } void Counter::run() { Q_EMIT textChanged("ZOMG"); } void Counter::OKalarmLimits() { Q_EMIT textChanged("Hello World"); }