Results 1 to 3 of 3

Thread: How pushbutton , qthreads works with Qdialog ?

  1. #1
    Join Date
    Jan 2015
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Question 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
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "configdialog.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. Q_INIT_RESOURCE(configdialog);
    8.  
    9. QApplication app(argc, argv);
    10. app.setApplicationDisplayName("Qt");
    11. ConfigDialog dialog;
    12. return dialog.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    pages.cpp
    Qt Code:
    1. #include <QtWidgets>
    2. #include "mythread.h"
    3. #include "pages.h"
    4. #include "mylineedit.h"
    5.  
    6. ConfigurationPage::ConfigurationPage(QWidget *parent)
    7. : QWidget(parent)
    8. {
    9. QGroupBox *packagesGroup = new QGroupBox(tr("Group-1"));
    10.  
    11.  
    12.  
    13. QGridLayout *packagesLayout = new QGridLayout;
    14.  
    15.  
    16.  
    17. packagesGroup->setLayout(packagesLayout);
    18. QVBoxLayout *mainLayout = new QVBoxLayout;
    19. mainLayout->addWidget(packagesGroup);
    20. QLineEdit *nameEdit = new QLineEdit;
    21. QLineEdit *nameEdit1 = new QLineEdit;
    22.  
    23. QPushButton *startQueryButton = new QPushButton(tr("one"));
    24. QPushButton *startQueryButton1 = new QPushButton(tr("two"));
    25.  
    26. MyThread thread;
    27. qDebug("Thread id %d",(int)QThread::currentThreadId());
    28. QObject::connect(startQueryButton,SIGNAL(clicked()),&thread,SLOT(start())) ;
    29. QObject::connect(&thread,SIGNAL(signalGUI(const QString&)),nameEdit,SLOT(setText(const QString&)));
    30.  
    31. MyThread1 thread1;
    32. qDebug("Thread id %d",(int)QThread::currentThreadId());
    33. QObject::connect(startQueryButton1,SIGNAL(clicked()),&thread1,SLOT(start())) ;
    34. QObject::connect(&thread,SIGNAL(signalGUI(const QString&)),nameEdit1,SLOT(setText(const QString&)));
    35.  
    36.  
    37. packagesLayout->addWidget(startQueryButton, 0 ,0);
    38. packagesLayout->addWidget(nameEdit, 0, 1);
    39. packagesLayout->addWidget(startQueryButton1, 1 ,0);
    40. packagesLayout->addWidget(nameEdit1, 1, 1);
    41.  
    42.  
    43.  
    44. setLayout(mainLayout);
    45.  
    46.  
    47. }
    48.  
    49. UpdatePage::UpdatePage(QWidget *parent)
    50. : QWidget(parent)
    51. {
    52. QGroupBox *packagesGroup = new QGroupBox(tr("group-2"));
    53.  
    54.  
    55.  
    56. QGridLayout *packagesLayout = new QGridLayout;
    57. packagesGroup->setLayout(packagesLayout);
    58. QVBoxLayout *mainLayout = new QVBoxLayout;
    59. mainLayout->addWidget(packagesGroup);
    60. QLineEdit *nameEdit = new QLineEdit;
    61.  
    62. QPushButton *startQueryButton = new QPushButton(tr("one"));
    63. packagesLayout->addWidget(startQueryButton, 0 ,0);
    64. packagesLayout->addWidget(nameEdit, 0, 1);
    65.  
    66.  
    67. setLayout(mainLayout);
    68.  
    69. }
    70.  
    71.  
    72. QueryPage::QueryPage(QWidget *parent)
    73. : QWidget(parent)
    74. {
    75. QGroupBox *packagesGroup = new QGroupBox(tr("group-3"));
    76.  
    77. QGridLayout *packagesLayout = new QGridLayout;
    78. packagesGroup->setLayout(packagesLayout);
    79. QVBoxLayout *mainLayout = new QVBoxLayout;
    80. mainLayout->addWidget(packagesGroup);
    81. QLineEdit *nameEdit = new QLineEdit;
    82. QPushButton *startQueryButton = new QPushButton(tr("one"));
    83. packagesLayout->addWidget(startQueryButton, 0 ,0);
    84. packagesLayout->addWidget(nameEdit, 0, 1);
    85.  
    86. setLayout(mainLayout);
    87.  
    88. }
    To copy to clipboard, switch view to plain text mode 
    pages.h
    Qt Code:
    1. #ifndef PAGES_H
    2. #define PAGES_H
    3.  
    4. #include <QWidget>
    5.  
    6. class ConfigurationPage : public QWidget
    7. {
    8. public:
    9. ConfigurationPage(QWidget *parent = 0);
    10. };
    11.  
    12. class QueryPage : public QWidget
    13. {
    14. public:
    15. QueryPage(QWidget *parent = 0);
    16. };
    17.  
    18. class UpdatePage : public QWidget
    19. {
    20. public:
    21. UpdatePage(QWidget *parent = 0);
    22. };
    23.  
    24.  
    25.  
    26. #endif
    To copy to clipboard, switch view to plain text mode 

    configdialog.cpp
    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. #include "configdialog.h"
    4. #include "pages.h"
    5.  
    6. ConfigDialog::ConfigDialog()
    7. {
    8. contentsWidget = new QListWidget;
    9. contentsWidget->setViewMode(QListView::IconMode);
    10. contentsWidget->setIconSize(QSize(96, 84));
    11. contentsWidget->setMovement(QListView::Static);
    12. contentsWidget->setMaximumWidth(128);
    13. contentsWidget->setSpacing(12);
    14.  
    15. pagesWidget = new QStackedWidget;
    16. pagesWidget->addWidget(new ConfigurationPage);
    17. pagesWidget->addWidget(new UpdatePage);
    18. pagesWidget->addWidget(new QueryPage);
    19.  
    20. QPushButton *closeButton = new QPushButton(tr("Close"));
    21.  
    22. createIcons();
    23. contentsWidget->setCurrentRow(0);
    24.  
    25. connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
    26.  
    27. QHBoxLayout *horizontalLayout = new QHBoxLayout;
    28. horizontalLayout->addWidget(contentsWidget);
    29. horizontalLayout->addWidget(pagesWidget, 1);
    30.  
    31. QHBoxLayout *buttonsLayout = new QHBoxLayout;
    32. buttonsLayout->addStretch(1);
    33. buttonsLayout->addWidget(closeButton);
    34.  
    35. QVBoxLayout *mainLayout = new QVBoxLayout;
    36. mainLayout->addLayout(horizontalLayout);
    37. mainLayout->addStretch(1);
    38. mainLayout->addSpacing(12);
    39. mainLayout->addLayout(buttonsLayout);
    40. setLayout(mainLayout);
    41.  
    42. setWindowTitle(tr("QdialogBox"));
    43. }
    44.  
    45. void ConfigDialog::createIcons()
    46. {
    47. QListWidgetItem *configButton = new QListWidgetItem(contentsWidget);
    48. configButton->setIcon(QIcon(":/images/config.png"));
    49. configButton->setText(tr("group-1"));
    50. configButton->setTextAlignment(Qt::AlignHCenter);
    51. configButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    52.  
    53. QListWidgetItem *updateButton = new QListWidgetItem(contentsWidget);
    54. updateButton->setIcon(QIcon(":/images/update.png"));
    55. updateButton->setText(tr("group-2"));
    56. updateButton->setTextAlignment(Qt::AlignHCenter);
    57. updateButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    58.  
    59. QListWidgetItem *queryButton = new QListWidgetItem(contentsWidget);
    60. queryButton->setIcon(QIcon(":/images/query.png"));
    61. queryButton->setText(tr("group-3"));
    62. queryButton->setTextAlignment(Qt::AlignHCenter);
    63. queryButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    64.  
    65. connect(contentsWidget,
    66. SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
    67. this, SLOT(changePage(QListWidgetItem*,QListWidgetItem*)));
    68. }
    69.  
    70. void ConfigDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous)
    71. {
    72. if (!current)
    73. current = previous;
    74.  
    75. pagesWidget->setCurrentIndex(contentsWidget->row(current));
    76. }
    To copy to clipboard, switch view to plain text mode 
    configdialog.h
    Qt Code:
    1. #ifndef CONFIGDIALOG_H
    2. #define CONFIGDIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6.  
    7. class ConfigDialog : public QDialog
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. ConfigDialog();
    13.  
    14. public slots:
    15. void changePage(QListWidgetItem *current, QListWidgetItem *previous);
    16.  
    17. private:
    18. void createIcons();
    19.  
    20. QListWidget *contentsWidget;
    21. QStackedWidget *pagesWidget;
    22. };
    23.  
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    mythread.cpp
    Qt Code:
    1. #include "mythread.h"
    2. #include <QString>
    3.  
    4. void MyThread::run()
    5. {
    6. qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
    7. static int run = 0;
    8.  
    9. QString temp = QString(": %1").arg(run++);
    10. qDebug("String address inside run %p", &temp);
    11. emit signalGUI(temp);
    12. }
    13.  
    14.  
    15. void MyThread1::run()
    16. {
    17. qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
    18. static int run = 0;
    19.  
    20. QString temp = QString(": %1").arg(run++);
    21. qDebug("String address inside run %p", &temp);
    22. emit signalGUI(temp);
    23. }
    To copy to clipboard, switch view to plain text mode 
    mythread.h
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QMutex>
    6.  
    7. class MyThread : public QThread
    8. {
    9. Q_OBJECT
    10.  
    11. protected:
    12. virtual void run();
    13.  
    14. signals:
    15. void signalGUI(QString);
    16. };
    17.  
    18.  
    19. class MyThread1 : public QThread
    20. {
    21. Q_OBJECT
    22.  
    23. protected:
    24. virtual void run();
    25.  
    26. signals:
    27. void signalGUI(QString);
    28. };
    29.  
    30. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 3rd March 2015 at 13:24. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

Similar Threads

  1. Replies: 3
    Last Post: 6th July 2011, 06:59
  2. Replies: 3
    Last Post: 29th April 2011, 08:54
  3. Replies: 9
    Last Post: 25th March 2011, 21:22
  4. QDialog.exec() exiting without calling QDialog::accept()
    By doggrant in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2011, 11:35
  5. Qthreads
    By Sheetal in forum Qt Programming
    Replies: 5
    Last Post: 23rd March 2007, 11:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.