PDA

View Full Version : How pushbutton , qthreads works with Qdialog ?



narthanl
3rd March 2015, 12:56
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


#include <QApplication>

#include "configdialog.h"

int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(configdialog);

QApplication app(argc, argv);
app.setApplicationDisplayName("Qt");
ConfigDialog dialog;
return dialog.exec();
}

pages.cpp


#include <QtWidgets>
#include "mythread.h"
#include "pages.h"
#include "mylineedit.h"

ConfigurationPage::ConfigurationPage(QWidget *parent)
: QWidget(parent)
{
QGroupBox *packagesGroup = new QGroupBox(tr("Group-1"));



QGridLayout *packagesLayout = new QGridLayout;



packagesGroup->setLayout(packagesLayout);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(packagesGroup);
QLineEdit *nameEdit = new QLineEdit;
QLineEdit *nameEdit1 = new QLineEdit;

QPushButton *startQueryButton = new QPushButton(tr("one"));
QPushButton *startQueryButton1 = new QPushButton(tr("two"));

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)
: QWidget(parent)
{
QGroupBox *packagesGroup = new QGroupBox(tr("group-2"));



QGridLayout *packagesLayout = new QGridLayout;
packagesGroup->setLayout(packagesLayout);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(packagesGroup);
QLineEdit *nameEdit = new QLineEdit;

QPushButton *startQueryButton = new QPushButton(tr("one"));
packagesLayout->addWidget(startQueryButton, 0 ,0);
packagesLayout->addWidget(nameEdit, 0, 1);


setLayout(mainLayout);

}


QueryPage::QueryPage(QWidget *parent)
: QWidget(parent)
{
QGroupBox *packagesGroup = new QGroupBox(tr("group-3"));

QGridLayout *packagesLayout = new QGridLayout;
packagesGroup->setLayout(packagesLayout);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(packagesGroup);
QLineEdit *nameEdit = new QLineEdit;
QPushButton *startQueryButton = new QPushButton(tr("one"));
packagesLayout->addWidget(startQueryButton, 0 ,0);
packagesLayout->addWidget(nameEdit, 0, 1);

setLayout(mainLayout);

}

pages.h



#ifndef PAGES_H
#define PAGES_H

#include <QWidget>

class ConfigurationPage : public QWidget
{
public:
ConfigurationPage(QWidget *parent = 0);
};

class QueryPage : public QWidget
{
public:
QueryPage(QWidget *parent = 0);
};

class UpdatePage : public QWidget
{
public:
UpdatePage(QWidget *parent = 0);
};



#endif


configdialog.cpp


#include <QtWidgets>

#include "configdialog.h"
#include "pages.h"

ConfigDialog::ConfigDialog()
{
contentsWidget = new QListWidget;
contentsWidget->setViewMode(QListView::IconMode);
contentsWidget->setIconSize(QSize(96, 84));
contentsWidget->setMovement(QListView::Static);
contentsWidget->setMaximumWidth(128);
contentsWidget->setSpacing(12);

pagesWidget = new QStackedWidget;
pagesWidget->addWidget(new ConfigurationPage);
pagesWidget->addWidget(new UpdatePage);
pagesWidget->addWidget(new QueryPage);

QPushButton *closeButton = new QPushButton(tr("Close"));

createIcons();
contentsWidget->setCurrentRow(0);

connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

QHBoxLayout *horizontalLayout = new QHBoxLayout;
horizontalLayout->addWidget(contentsWidget);
horizontalLayout->addWidget(pagesWidget, 1);

QHBoxLayout *buttonsLayout = new QHBoxLayout;
buttonsLayout->addStretch(1);
buttonsLayout->addWidget(closeButton);

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(horizontalLayout);
mainLayout->addStretch(1);
mainLayout->addSpacing(12);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);

setWindowTitle(tr("QdialogBox"));
}

void ConfigDialog::createIcons()
{
QListWidgetItem *configButton = new QListWidgetItem(contentsWidget);
configButton->setIcon(QIcon(":/images/config.png"));
configButton->setText(tr("group-1"));
configButton->setTextAlignment(Qt::AlignHCenter);
configButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

QListWidgetItem *updateButton = new QListWidgetItem(contentsWidget);
updateButton->setIcon(QIcon(":/images/update.png"));
updateButton->setText(tr("group-2"));
updateButton->setTextAlignment(Qt::AlignHCenter);
updateButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

QListWidgetItem *queryButton = new QListWidgetItem(contentsWidget);
queryButton->setIcon(QIcon(":/images/query.png"));
queryButton->setText(tr("group-3"));
queryButton->setTextAlignment(Qt::AlignHCenter);
queryButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

connect(contentsWidget,
SIGNAL(currentItemChanged(QListWidgetItem*,QListWi dgetItem*)),
this, SLOT(changePage(QListWidgetItem*,QListWidgetItem*) ));
}

void ConfigDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous)
{
if (!current)
current = previous;

pagesWidget->setCurrentIndex(contentsWidget->row(current));
}

configdialog.h


#ifndef CONFIGDIALOG_H
#define CONFIGDIALOG_H

#include <QDialog>

class QListWidget;
class QListWidgetItem;
class QStackedWidget;

class ConfigDialog : public QDialog
{
Q_OBJECT

public:
ConfigDialog();

public slots:
void changePage(QListWidgetItem *current, QListWidgetItem *previous);

private:
void createIcons();

QListWidget *contentsWidget;
QStackedWidget *pagesWidget;
};

#endif


mythread.cpp


#include "mythread.h"
#include <QString>

void MyThread::run()
{
qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
static int run = 0;

QString temp = QString(": %1").arg(run++);
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;

QString temp = QString(": %1").arg(run++);
qDebug("String address inside run %p", &temp);
emit signalGUI(temp);
}


mythread.h


#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QMutex>

class MyThread : public QThread
{
Q_OBJECT

protected:
virtual void run();

signals:
void signalGUI(QString);
};


class MyThread1 : public QThread
{
Q_OBJECT

protected:
virtual void run();

signals:
void signalGUI(QString);
};

#endif // MYTHREAD_H

anda_skoa
3rd March 2015, 13:17
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,
_

anda_skoa
6th March 2015, 09:37
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,
_