How pushbutton , qthreads works with Qdialog ?
hi i am new to Qt, i created a Qdialog with PushButton and threads, i am is to create whenever i press the push button it executes the thread, but i failed to do that., will someone please explain how to do that , Thank you
main.cpp
Code:
#include <QApplication>
#include "configdialog.h"
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(configdialog);
app.setApplicationDisplayName("Qt");
ConfigDialog dialog;
return dialog.exec();
}
pages.cpp
Code:
#include <QtWidgets>
#include "mythread.h"
#include "pages.h"
#include "mylineedit.h"
ConfigurationPage
::ConfigurationPage(QWidget *parent
){
packagesGroup->setLayout(packagesLayout);
mainLayout->addWidget(packagesGroup);
MyThread thread;
qDebug
("Thread id %d",
(int)QThread::currentThreadId());
QObject::connect(startQueryButton,
SIGNAL(clicked
()),
&thread,
SLOT(start
())) ;
QObject::connect(&thread,
SIGNAL(signalGUI
(const QString
&)),nameEdit,
SLOT(setText
(const QString
&)));
MyThread1 thread1;
qDebug
("Thread id %d",
(int)QThread::currentThreadId());
QObject::connect(startQueryButton1,
SIGNAL(clicked
()),
&thread1,
SLOT(start
())) ;
QObject::connect(&thread,
SIGNAL(signalGUI
(const QString
&)),nameEdit1,
SLOT(setText
(const QString
&)));
packagesLayout->addWidget(startQueryButton, 0 ,0);
packagesLayout->addWidget(nameEdit, 0, 1);
packagesLayout->addWidget(startQueryButton1, 1 ,0);
packagesLayout->addWidget(nameEdit1, 1, 1);
setLayout(mainLayout);
}
UpdatePage
::UpdatePage(QWidget *parent
){
packagesGroup->setLayout(packagesLayout);
mainLayout->addWidget(packagesGroup);
packagesLayout->addWidget(startQueryButton, 0 ,0);
packagesLayout->addWidget(nameEdit, 0, 1);
setLayout(mainLayout);
}
QueryPage
::QueryPage(QWidget *parent
){
packagesGroup->setLayout(packagesLayout);
mainLayout->addWidget(packagesGroup);
packagesLayout->addWidget(startQueryButton, 0 ,0);
packagesLayout->addWidget(nameEdit, 0, 1);
setLayout(mainLayout);
}
pages.h
Code:
#ifndef PAGES_H
#define PAGES_H
#include <QWidget>
class ConfigurationPage
: public QWidget{
public:
ConfigurationPage
(QWidget *parent
= 0);
};
{
public:
};
{
public:
};
#endif
configdialog.cpp
Code:
#include <QtWidgets>
#include "configdialog.h"
#include "pages.h"
ConfigDialog::ConfigDialog()
{
contentsWidget
->setViewMode
(QListView::IconMode);
contentsWidget
->setIconSize
(QSize(96,
84));
contentsWidget
->setMovement
(QListView::Static);
contentsWidget->setMaximumWidth(128);
contentsWidget->setSpacing(12);
pagesWidget->addWidget(new ConfigurationPage);
pagesWidget->addWidget(new UpdatePage);
pagesWidget->addWidget(new QueryPage);
createIcons();
contentsWidget->setCurrentRow(0);
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
horizontalLayout->addWidget(contentsWidget);
horizontalLayout->addWidget(pagesWidget, 1);
buttonsLayout->addStretch(1);
buttonsLayout->addWidget(closeButton);
mainLayout->addLayout(horizontalLayout);
mainLayout->addStretch(1);
mainLayout->addSpacing(12);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
setWindowTitle(tr("QdialogBox"));
}
void ConfigDialog::createIcons()
{
configButton
->setIcon
(QIcon(":/images/config.png"));
configButton->setText(tr("group-1"));
configButton->setTextAlignment(Qt::AlignHCenter);
configButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
updateButton
->setIcon
(QIcon(":/images/update.png"));
updateButton->setText(tr("group-2"));
updateButton->setTextAlignment(Qt::AlignHCenter);
updateButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
queryButton
->setIcon
(QIcon(":/images/query.png"));
queryButton->setText(tr("group-3"));
queryButton->setTextAlignment(Qt::AlignHCenter);
queryButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
connect(contentsWidget,
}
{
if (!current)
current = previous;
pagesWidget->setCurrentIndex(contentsWidget->row(current));
}
configdialog.h
Code:
#ifndef CONFIGDIALOG_H
#define CONFIGDIALOG_H
#include <QDialog>
class ConfigDialog
: public QDialog{
Q_OBJECT
public:
ConfigDialog();
public slots:
private:
void createIcons();
};
#endif
mythread.cpp
Code:
#include "mythread.h"
#include <QString>
void MyThread::run()
{
qDebug
("Thread id inside run %d",
(int)QThread::currentThreadId());
static int run = 0;
qDebug("String address inside run %p", &temp);
emit signalGUI(temp);
}
void MyThread1::run()
{
qDebug
("Thread id inside run %d",
(int)QThread::currentThreadId());
static int run = 0;
qDebug("String address inside run %p", &temp);
emit signalGUI(temp);
}
mythread.h
Code:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QMutex>
{
Q_OBJECT
protected:
virtual void run();
signals:
};
{
Q_OBJECT
protected:
virtual void run();
signals:
};
#endif // MYTHREAD_H
Re: how Pushbutton and Qthread work with Qdialog
Posting the same thing twice within ~10 minutes is surely going to improve the response rate.
Posting lots of code without code tags is also really helpful.
Regarding your problem: understanding C++ basics, like function scopes and object life time, is kind of a requirement for using Qt, it is a C++ library.
Cheers,
_
Re: how Pushbutton and Qthread work with Qdialog
Since you apparently refuse to look at your code yourself to find the location where your lack of basic understanding of C++ causes the problem: you create the two thread objects in the stack scope of the ConfigurationPage constructor. Once that scope ends, your thread objects are gone.
Cheers,
_