failure to initialize multiple windows using qt - qt

I've been struggling with the problem of initializing of multiple forms in Qt under Windows (the version is 5.0.1 but I don't reckon this to matter much). Long story short, my application contains of 2 forms the one of which is supposed to be called on clicking the button on the another one.
The code runs as follows:
1) main.cpp:
#include "main.h"
#include <QApplication>
FmNewWord fmNewWord;
MainWindow w;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
w.show();
return a.exec();
}
2) main.h:
#ifndef MAIN_H
#define MAIN_H
#include "mainwindow.h"
#include "fmnewword.h"
extern FmNewWord fmNewWord;
extern MainWindow w;
#endif // MAIN_H
3) mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "ui_mainwindow.h"
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
signals:
void sgnl_new_word();
public:
explicit MainWindow(QWidget *parent = 0);
protected:
void changeEvent(QEvent *e);
private slots:
void on_commandLinkButton_clicked();
void on_miExit_triggered();
void on_miNewWord_triggered();
};
#endif // MAINWINDOW_H
4) mainwindow.cpp:
#include "main.h"
#include <Windows.h>
extern FmNewWord fmNewWord;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_commandLinkButton_clicked()
{
MessageBox(0, 0, 0, 0);
}
void MainWindow::on_miExit_triggered()
{
this->close();
}
void MainWindow::on_miNewWord_triggered()
{
fmNewWord.show();
}
5) fmnewword.cpp:
#include "fmnewword.h"
FmNewWord::FmNewWord(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this); /// here the app crashes with parent == NULL
}
void FmNewWord::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi(this);
break;
default:
break;
}
}
6) fmnewword.h:
#ifndef FMNEWWORD_H
#define FMNEWWORD_H
#include "ui_fmnewword.h"
class FmNewWord : public QMainWindow, private Ui::FmNewWord
{
Q_OBJECT
public:
explicit FmNewWord(QWidget *parent = 0);
protected:
void changeEvent(QEvent *e);
};
#endif // FMNEWWORD_H
The code keeps crashing during inititialization in fmnewword.cpp on the line shown. Could anybody point out the reason? Thanks.

First of all, QApplication must be created before any widgets.
Your code should look llike this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
And that's how the second form must be created:
class FmNewWord;
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
...
private slots:
void on_miNewWord_triggered();
private:
FmNewWord *fmNewWord;
};
void MainWindow::on_miNewWord_triggered()
{
fmNewWord = new FmNewWord;
fmNewWord->show();
}

Related

Couldn't render OpenGLES context in Qt

I am trying to use OpenGLES in a desktop platform. I have a class whose name is GraphicsViewer which is derived from QOpenGLWidget. When I set the QSurfaceFormat::renderableType to QSurfaceFormat::OpenGLES, I get the following errors and nothing is drawn on the widget. Can you help me to create an OpenGLES context in a desktop platform? My OS is Windows 10 and my Qt version is 5.12.3.
Errors:
QOpenGLWidget: Failed to create context
QOpenGLWidget: Failed to create context
qt.qpa.backingstore: composeAndFlush: QOpenGLContext creation failed
QOpenGLWidget: Failed to create context
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed
Here is my codes...
main.cpp
#include "QtOpenGLES.h"
#include <QApplication>
#include <QSurfaceFormat>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSurfaceFormat f;
f.setVersion(2, 0);
f.setRenderableType(QSurfaceFormat::OpenGLES);
QSurfaceFormat::setDefaultFormat(f);
QtOpenGLES w;
w.show();
return a.exec();
}
QtOpenGLES.h
#ifndef QTOPENGLES_H
#define QTOPENGLES_H
#include <QMainWindow>
namespace Ui {
class QtOpenGLES;
}
class QtOpenGLES : public QMainWindow
{
Q_OBJECT
public:
explicit QtOpenGLES(QWidget *parent = nullptr);
~QtOpenGLES();
private:
Ui::QtOpenGLES *ui;
};
#endif // QTOPENGLES_H
QtOpenGLES.cpp
#include "QtOpenGLES.h"
#include "ui_QtOpenGLES.h"
QtOpenGLES::QtOpenGLES(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::QtOpenGLES)
{
ui->setupUi(this);
}
QtOpenGLES::~QtOpenGLES()
{
delete ui;
}
GraphicsViewer.h
#ifndef GRAPHICSVIEWER_H
#define GRAPHICSVIEWER_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
class GraphicsViewer : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
GraphicsViewer(QWidget* parent = nullptr);
// QOpenGLWidget interface
protected:
void initializeGL();
void paintGL();
};
GraphicsViewer.cpp
#include "GraphicsViewer.h"
#include <QtOpenGL>
GraphicsViewer::GraphicsViewer(QWidget *parent) :
QOpenGLWidget (parent)
{
}
void GraphicsViewer::initializeGL()
{
initializeOpenGLFunctions();
}
void GraphicsViewer::paintGL()
{
glClearColor(1, 0, 0, 1);
}

How to properly stop QTimer from another thread

I see that such topic was discussed many times, but can't find clear answer to simple situation. I have Worker class running in the own thread where I create timer and want to stop it due to some condition.
But I am getting the error:
Timers cannot be stopped from another thread
I fill that am missing some core logic in thread working in Qt. Can someone please explain how to resolve that? Thank you.
Here is the main.cpp
#include <QCoreApplication>
#include <QObject>
#include <QtDebug>
#include "worker.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Worker w;
return a.exec();
}
Worker.h
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include <QTimer>
#include <QThread>
#include <QtDebug>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = nullptr);
private:
QThread t;
QTimer *timer;
int count = 1;
public slots:
void dataTimerFunction();
void onStopTimer(QTimer *t);
signals:
void stopTimer(QTimer *t);
};
#endif // WORKER_H
Worker.cpp
#include "worker.h"
Worker::Worker(QObject *parent) : QObject(parent)
{
this->moveToThread(&t);
QObject::connect(&t, &QThread::finished, this, &QObject::deleteLater);
t.start();
// are we in the new thread from this point, right?
timer = new QTimer();
QObject::connect(timer, &QTimer::timeout, this, &Worker::dataTimerFunction);
QObject::connect(this, &Worker::stopTimer, this, &Worker::onStopTimer);
// QObject::connect(this, &Worker::stopTimer, this, &Worker::onStopTimer, Qt::QueuedConnection); doesn't work as well
timer->start(200);
}
void Worker::dataTimerFunction()
{
qDebug()<<count;
count++;
if (count>5){
emit stopTimer(timer);
//timer->stop();
}
}
void Worker::onStopTimer(QTimer *t)
{
t->stop();
}
The problem is indeed that the timer is not moved to your thread.
Here is a modified worker class, that does what you might want to achieve. It moves the timer also to the thread and then starts and stops it by using signals / slots.
worker.h
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include <QTimer>
#include <QThread>
#include <QtDebug>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = nullptr);
private:
QThread t;
QTimer *timer;
int count = 1;
signals:
void stopTimer();
void startTimer(int msec);
public slots:
void dataTimerFunction();
};
#endif // WORKER_H
worker.cpp
#include "worker.h"
Worker::Worker(QObject *parent) : QObject(parent)
{
// are we in the new thread from this point, right?
timer = new QTimer(nullptr);
this->moveToThread(&t);
timer->moveToThread(&t);
QObject::connect(&t, &QThread::finished, this, &QObject::deleteLater);
t.start();
QObject::connect(timer, &QTimer::timeout, this, &Worker::dataTimerFunction);
QObject::connect(this, &Worker::stopTimer, timer, &QTimer::stop, Qt::QueuedConnection);
QObject::connect(this, &Worker::startTimer, timer, static_cast<void (QTimer::*)(int)>(&QTimer::start), Qt::QueuedConnection);
startTimer(200);
}
void Worker::dataTimerFunction()
{
qDebug()<<count;
count++;
if (count>5){
stopTimer();
}
}
You should new your timer in your task object slot, not in the task construtor. Stop the timer when your task object is deleted.
My task object header: Worker.h
#pragma once
#include <QObject>
class QTimer;
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = nullptr);
~Worker();
public slots:
void WorkerTaskStartSlot(void);
void TaskFinished(void);
private slots:
void TimerOutToDoSomethingSlot(void);
signals:
void WorkertResultSig(void);
private:
QTimer *m_pTimer = nullptr;
};
Worker.cpp:
#include "Worker.h"
#include <QDebug>
#include <QThread>
#include <QTimer>
Worker::Worker(QObject *parent) : QObject(parent)
{
qDebug()<<__FUNCTION__<<"threadid"<< QThread::currentThreadId();
}
Worker::~Worker()
{
TaskFinished();
qDebug()<<__FUNCTION__<<"threadid"<< QThread::currentThreadId();
}
void Worker::WorkerTaskStartSlot(void)
{
qDebug()<<__FUNCTION__<<"threadid"<< QThread::currentThreadId();
emit WorkertResultSig();
m_pTimer = new QTimer(this);
connect(m_pTimer,&QTimer::timeout,this,&Worker::TimerOutToDoSomethingSlot);
m_pTimer->start(1000);
}
void Worker::TaskFinished(void)
{
qDebug()<<__FUNCTION__<<"threadid"<< QThread::currentThreadId();
if(m_pTimer)
{
if(m_pTimer->isActive())
{
m_pTimer->stop();
qDebug()<<__FUNCTION__<<"stop timer"<< QThread::currentThreadId();
}
delete m_pTimer;
m_pTimer = nullptr;
}
}
void Worker::TimerOutToDoSomethingSlot(void)
{
qDebug()<<__FUNCTION__<<"threadid"<< QThread::currentThreadId();
}
The controller header: MainWindow.h
#pragma once
#include <QMainWindow>
class QPushButton;
class QWidget;
class Worker;
class QThread;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
void InitCtrl(void);
private slots:
void StartTaskBtnSlot(const bool &checked);
void WorkertResultSlot(void);
private:
QPushButton *m_pStartTaskBtn = nullptr;
QWidget *m_pCenterWidget = nullptr;
Worker *m_pWorker = nullptr;
QThread *m_pWorkerThread = nullptr;
};
MainWindow.cpp
#include "MainWindow.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QWidget>
#include <QDebug>
#include <QThread>
#include "Worker.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
InitCtrl();
qDebug()<<__FUNCTION__<<"mainwindow thread id"<< QThread::currentThreadId();
}
MainWindow::~MainWindow()
{
}
void MainWindow::InitCtrl(void)
{
m_pCenterWidget = new QWidget(this);
m_pStartTaskBtn = new QPushButton(QStringLiteral("Start"),this);
QVBoxLayout *pvertlayout = new QVBoxLayout();
pvertlayout->addWidget(m_pStartTaskBtn);
m_pCenterWidget->setLayout(pvertlayout);
setCentralWidget(m_pCenterWidget);
m_pStartTaskBtn->setCheckable(true);
connect(m_pStartTaskBtn,&QPushButton::clicked,this,&MainWindow::StartTaskBtnSlot);
}
void MainWindow::StartTaskBtnSlot(const bool &checked)
{
if(checked)
{
m_pStartTaskBtn->setText(QStringLiteral("Close"));
m_pWorkerThread = new QThread();
m_pWorker = new Worker();
// move the task object to the thread BEFORE connecting any signal/slots
m_pWorker->moveToThread(m_pWorkerThread);
connect(m_pWorkerThread, SIGNAL(started()), m_pWorker, SLOT(WorkerTaskStartSlot()));
connect(m_pWorker, SIGNAL(WorkertResultSig()), this, SLOT(WorkertResultSlot()));
// automatically delete thread and task object when work is done:
connect(m_pWorkerThread, SIGNAL(finished()), m_pWorker, SLOT(deleteLater()));
connect(m_pWorkerThread, SIGNAL(finished()), m_pWorkerThread, SLOT(deleteLater()));
m_pWorkerThread->start();
}
else
{
m_pStartTaskBtn->setText(QStringLiteral("Start"));
m_pWorkerThread->quit();
m_pWorkerThread->wait();
}
}
void MainWindow::WorkertResultSlot(void)
{
qDebug()<<__FUNCTION__<<"threadid"<<QThread::currentThreadId();
}
Finally, it will output result like this:
MainWindow::MainWindow mainwindow thread id 0x2bf0
Worker::Worker threadid 0x2bf0
Worker::WorkerTaskStartSlot threadid 0x4af0
MainWindow::WorkertResultSlot threadid 0x2bf0
Worker::TimerOutToDoSomethingSlot threadid 0x4af0
Worker::TimerOutToDoSomethingSlot threadid 0x4af0
Worker::TimerOutToDoSomethingSlot threadid 0x4af0
Worker::TimerOutToDoSomethingSlot threadid 0x4af0
Worker::TaskFinished threadid 0x4af0
Worker::TaskFinished stop timer 0x4af0
Worker::~Worker threadid 0x4af0

How to reuse the same window after clicking on other button?

I am trying to create an image viewer with slide show. When the user clicks on the play button, the viewer starts to show images. When the the user clicks on the stop button, the viewer stops to show images. After stopping, when the user clicks on the play button again, the viewer will continue to show the remaining images. My problem is that when the user clicks on the stop button and click on the play button again, I don't know how to reuse the same window created at the beginning to show the remaining images.
Button3 is the play button.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileSystemModel>
#include "showpic.h"
#include <QBasicTimer>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_3_clicked();
void tick();
void on_pushButton_4_clicked();
private:
Ui::MainWindow *ui;
QFileSystemModel *model;
QString filesPath;
ShowPic *showpic;
QStringList filenames;
QStringList::const_iterator m_imageIt;
QTimer m_timer;
};
#endif // MAINWINDOW_H
showpic.h
#ifndef SHOWPIC_H
#define SHOWPIC_H
#include <QWidget>
namespace Ui {
class ShowPic;
}
class ShowPic : public QWidget
{
Q_OBJECT
public:
explicit ShowPic(QWidget *parent = 0);
~ShowPic();
private:
Ui::ShowPic *ui;
public:
void addPixmap(const QPixmap &pixmap);
};
#endif // SHOWPIC_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QFileDialog>
#include<QFileSystemModel>
#include<QStringList>
#include <QTreeView>
#include <QGraphicsScene>
#include <QTime>
#include <QDebug>
#include <iostream>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
filenames.append("C:\\test\\image.jpg");
filenames.append("C:\\test\\apple.jpg");
filenames.append("C:\\test\\orange.jpg");
filenames.append("C:\\test\\lemon.jpg");
filenames.append("C:\\test\\grape.jpg");
m_timer.setInterval(1000);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::tick(){
showpic->addPixmap(*m_imageIt);
m_imageIt ++;
if(m_imageIt == filenames.end()){
m_timer.stop();
m_imageIt = filenames.begin();
}
}
void MainWindow::on_pushButton_3_clicked() //click on the play button
{
if(!filenames.isEmpty()){ // initial click
showpic = new ShowPic();
m_timer.start();
showpic->setWindowState(Qt::WindowMaximized);
showpic->show();
} else if( ) { // click on the play button again,
m_timer.start(); //???
showpic->setWindowState(Qt::WindowMaximized); //???
showpic->show(); //???
}
}
void MainWindow::on_pushButton_4_clicked() // click on the stop button
{
m_timer.stop();
}
showpic.cpp
#include "showpic.h"
#include "ui_showpic.h"
ShowPic::ShowPic(QWidget *parent) :
QWidget(parent),
ui(new Ui::ShowPic)
{
ui->setupUi(this);
ui->graphicsView->setScene(new QGraphicsScene);
ui->horizontalLayout->addWidget(ui->graphicsView);
this->setLayout(ui->horizontalLayout);
}
ShowPic::~ShowPic()
{
delete ui;
}
void ShowPic::addPixmap(const QPixmap &pixmap){
ui->horizontalLayout->addWidget(ui->graphicsView);
this->setLayout(ui->horizontalLayout);
ui->graphicsView->setScene(new QGraphicsScene);
ui->graphicsView->scene()->addPixmap(pixmap);
ui->graphicsView->fitInView(ui->graphicsView->scene()->itemsBoundingRect() ,Qt::KeepAspectRatio);
}
Else part of on_pushButton_3_clicked won't run in any case because filenames is being filled when MainWindow created. In your code, you're creating new showpic in every click.
Firstly, set showpic to NULL in constructor of MainWindow;
showpic = NULL;
And change on_pushButton_3_clicked method like this;
void MainWindow::on_pushButton_3_clicked() //click on the play button
{
if(showpic == NULL){
showpic = new ShowPic();
}
if(!showpic->isVisible()){
showpic->setWindowState(Qt::WindowMaximized);
showpic->show();
}
m_timer.start();
}
Lastly, i don't have QT now, so my answer may contains typo/syntax error.
This works for me. I tested the scene clearing method, and it worked fine for me. But here I decided to just store the pointer to the QGraphicsPixmapItem as a member variable, and just set a new pixmap to it, instead of clearing the scene constantly. Seems more elegant like this to me.
#include <QtWidgets>
class SlideView : public QWidget
{
Q_OBJECT
public:
SlideView(QWidget *parent = nullptr) : QWidget(parent)
{
setLayout(new QHBoxLayout);
layout()->addWidget(&view);
view.setScene(new QGraphicsScene(this));
pixmap_item = new QGraphicsPixmapItem;
view.scene()->addItem(pixmap_item);
}
void setPixmap(const QPixmap &pixmap)
{
pixmap_item->setPixmap(pixmap);
view.fitInView(view.scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
}
private:
QGraphicsView view;
QGraphicsPixmapItem *pixmap_item = nullptr;
};
class MainWidget : public QWidget
{
Q_OBJECT
public:
MainWidget(QWidget *parent = nullptr) : QWidget(parent)
{
slide_iterator = slides.begin();
setLayout(new QHBoxLayout);
QPushButton *play_button = new QPushButton("Play");
QPushButton *stop_button = new QPushButton("Stop");
layout()->addWidget(play_button);
layout()->addWidget(stop_button);
connect(play_button, &QPushButton::clicked, this, &MainWidget::play);
connect(stop_button, &QPushButton::clicked, this, &MainWidget::stop);
connect(&timer, &QTimer::timeout, this, &MainWidget::showNextSlide);
}
public slots:
void play() {timer.start(1000); view.showMaximized(); view.activateWindow();}
void stop() {timer.stop();}
void showNextSlide()
{
QPixmap pixmap(*slide_iterator);
view.setPixmap(pixmap);
slide_iterator++;
if(slide_iterator == slides.end())
slide_iterator = slides.begin();
}
private:
QTimer timer;
QStringList slides{"one.png", "two.png", "three.png"};
QStringList::const_iterator slide_iterator;
SlideView view;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWidget w;
w.show();
return a.exec();
}
#include "main.moc"

Thread refuses to quit [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Source: https://mega.co.nz/#!xUMiSRYC!Y8Sxz2fEFb6yEIrnKiGx9n2zeK4YTUwUrCByaAkcOPI
I'm experiencing a problem with QThread::currentThread()->quit();
If you open up my myobject.cpp and go to line 13, the quit() call is supposed to exit. But it seems the quit() call isn't working and it just skips it. Can't see the problem. :(
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QObject::connect(&a, SIGNAL(aboutToQuit()),
&w, SLOT(cleanUp()));
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myobject.h"
#include <QMessageBox>
MyObject* cObject;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setFixedSize(this->size());
cThread = new QThread(this);
cObject = new MyObject();
cObject->moveToThread(cThread);
QObject::connect(ui->pushButton_2, SIGNAL(clicked()),
this, SLOT(close()));
QObject::connect(cThread, SIGNAL(started()),
cObject, SLOT(doWork()));
QObject::connect(ui->pushButton, SIGNAL(clicked()),
this, SLOT(runThreadSlot()));
QObject::connect(cThread, SIGNAL(finished()),
cThread, SLOT(deleteLater()));
QObject::connect(cThread, SIGNAL(finished()),
cObject, SLOT(deleteLater()));
QObject::connect(cObject, SIGNAL(setStatusBarSignal(QString)),
this, SLOT(setStatusBarSlot(QString)));
ui->lineEdit->setValidator(new QIntValidator(this));
ui->lineEdit->setFocus(Qt::OtherFocusReason);
ui->statusBar->showMessage("Waiting for input...");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::runThreadSlot()
{
cThread->start();
// I've trimmed some code here.
}
void MainWindow::setStatusBarSlot(QString text)
{
ui->statusBar->showMessage(text);
}
void MainWindow::cleanUp()
{
qDebug() << "Cleaning up!";
cObject->stage = 0;
cThread->wait();
}
myobject.cpp:
#include "myobject.h"
#include <QPixmap>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
MyObject::MyObject(QObject *parent) :
QObject(parent)
{
}
void MyObject::doWork()
{
QThread::currentThread()->quit(); // It doesn't stop here. This is my issue.
// I've trimmed the code here.
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void runThreadSlot();
void setStatusBarSlot(QString);
void cleanUp();
private:
Ui::MainWindow *ui;
QThread* cThread;
};
#endif // MAINWINDOW_H
myobject.h:
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QtCore>
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject *parent = 0);
QFile path;
qint32 waitSecs;
int stage;
signals:
void setStatusBarSignal(QString);
public slots:
void doWork();
};
#endif // MYOBJECT_H
QThread::quit() is asynchronous. You shoul add QThread::currentThread()->wait() right after quit.

Parent and Child Window Communication in Qt - Child Window opens twice

I have two windows, one parent and one child. In parent window, I have a Next button, which onClick()'ed, opens up child window, but in my case two child windows are opening, what is the mistake am doing!?
Here are my codes:
.h files
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <info.h>
#include <QtGui>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
void setSignals();
private slots:
void process();
};
#endif // MAINWINDOW_H
info.h
#ifndef INFO_H
#define INFO_H
#include <QMainWindow>
namespace Ui {
class info;
}
class info : public QMainWindow {
Q_OBJECT
public:
info(QWidget *parent = 0);
~info();
protected:
void changeEvent(QEvent *e);
private:
Ui::info *ui;
};
#endif // INFO_H
.cpp files
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui/QApplication>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setSignals();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::setSignals(){
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(process()));
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(close()));
}
void MainWindow::process(){
info *i;
i = new info;
this -> hide();
i -> show();
}
info.cpp
#include "info.h"
#include "ui_info.h"
info::info(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::info)
{
ui->setupUi(this);
}
info::~info()
{
delete ui;
}
void info::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
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();
}
I solved it, here was the problem:
In designer header file i.e., in ui_mainwindow.h, I have:
QObject::connect(pushButton, SIGNAL(clicked()), MainWindow, SLOT(process()));
QObject::connect(pushButton_2, SIGNAL(clicked()), MainWindow, SLOT(close()));
and in the source file i.e., mainwindow.cpp in setSignals(), I have again stated:
void MainWindow::setSignals()
{
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(process()));
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(close()));
}
So with the two connects, we get two calls to process() , we have to comment anyone to show only one child window. That's it :-)

Resources