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);
app.setApplicationDisplayName("Qt");
ConfigDialog dialog;
return dialog.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
pages.cpp
#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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
pages.h
#ifndef PAGES_H
#define PAGES_H
#include <QWidget>
class ConfigurationPage
: public QWidget{
public:
ConfigurationPage
(QWidget *parent
= 0);
};
{
public:
};
{
public:
};
#endif
#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
To copy to clipboard, switch view to plain text mode
configdialog.cpp
#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));
}
#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*,QListWidgetItem*)),
this, SLOT(changePage(QListWidgetItem*,QListWidgetItem*)));
}
void ConfigDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous)
{
if (!current)
current = previous;
pagesWidget->setCurrentIndex(contentsWidget->row(current));
}
To copy to clipboard, switch view to plain text mode
configdialog.h
#ifndef CONFIGDIALOG_H
#define CONFIGDIALOG_H
#include <QDialog>
class ConfigDialog
: public QDialog{
Q_OBJECT
public:
ConfigDialog();
public slots:
private:
void createIcons();
};
#endif
#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
To copy to clipboard, switch view to plain text mode
mythread.cpp
#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);
}
#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);
}
To copy to clipboard, switch view to plain text mode
mythread.h
#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
#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
To copy to clipboard, switch view to plain text mode
Bookmarks