Results 1 to 5 of 5

Thread: Newbie Help - Signals/Slots between dialog and widgets

  1. #1
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Newbie Help - Signals/Slots between dialog and widgets

    Hello,

    I have a dialog which contains one QTabWidget consisting of several tabs. Each tab has its own class and I am adding a calculate button to each tab page. But when the user selects calculate, nothing happens. For now, I have a test message box that should indicate that the button was pressed, but it never hits the function, which debug confirms. The class of the widget is as follows:

    Qt Code:
    1. class AcreTab : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. AcreTab(QWidget *parent=0);
    7. QPushButton *pbAcreCalc;
    8.  
    9. };
    To copy to clipboard, switch view to plain text mode 

    and the dialog class is as follows:

    Qt Code:
    1. class Dialog : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Dialog(QWidget *parent = 0);
    7. ~Dialog();
    8.  
    9. private slots:
    10. void AcreCalc();
    11.  
    12. private:
    13. QTabWidget *tabWidget;
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 

    The signal/slot mechanism as well as tab setup is as follows:

    Qt Code:
    1. Dialog::Dialog(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. // Setup the tabs...
    5.  
    6. tabWidget = new QTabWidget();
    7. tabWidget->addTab(new AcreTab, tr("Acre"));
    8. tabWidget->addTab(new acresTab, tr("acres"));
    9. tabWidget->addTab(new amperesTab, tr("amperes"));
    10. tabWidget->addTab(new ampereHrsTab, tr("ampere-hours"));
    11. tabWidget->addTab(new ampereTurnsTab, tr("ampere-turns"));
    12. tabWidget->addTab(new atmosTab, tr("atmospheres"));
    13. tabWidget->addTab(new oilBarrel, tr("barrel, oil"));
    14. tabWidget->addTab(new barTab, tr("bars"));
    15. tabWidget->addTab(new btuTab, tr("btu"));
    16. tabWidget->addTab(new btuHrsTab, tr("btu/hrs"));
    17. tabWidget->addTab(new btuMinTab, tr("btu/min"));
    18. tabWidget->addTab(new bushelsTab, tr("bushels"));
    19. tabWidget->addTab(new cmTab, tr("centimetres"));
    20. tabWidget->addTab(new cmDynesTab, tr("centimetre-dynes"));
    21. tabWidget->addTab(new cmGramsTab, tr("centimetre-grams"));
    22. tabWidget->addTab(new cmMercTab, tr("centimetre-grams"));
    23. tabWidget->addTab(new cmSecTab, tr("centimetre-sec"));
    24. tabWidget->addTab(new cmCubeTab, tr("cubic centimetres"));
    25. tabWidget->addTab(new cubicFtTab, tr("cubic feet"));
    26. tabWidget->addTab(new cubicFtMinTab, tr("cubic feet/min"));
    27. tabWidget->addTab(new cubicInchesTab, tr("cubic inches"));
    28. tabWidget->addTab(new cubicMetresTab, tr("cubic metres"));
    29.  
    30. AcreTab acrTb;
    31. connect(acrTb.pbAcreCalc, SIGNAL(clicked()),this, SLOT(AcreCalc()));
    32.  
    33.  
    34. //Layout the dialog box...
    35.  
    36. QVBoxLayout *mLayout = new QVBoxLayout;
    37. mLayout->addWidget(tabWidget);
    38. setLayout(mLayout);
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 

    And the tabs are created by:

    Qt Code:
    1. AcreTab::AcreTab(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4.  
    5. QLabel *lblConvertFrom = new QLabel(tr("Convert From: "));
    6. QLineEdit *leConvFrom = new QLineEdit();
    7. QLabel *lblConvertTo = new QLabel(tr("Convert To: "));
    8. QLineEdit *leConvTo = new QLineEdit();
    9.  
    10. QLabel *lblAcreConv = new QLabel(tr("Answer: "));
    11. lblAcreConv->setAlignment(Qt::AlignRight);
    12. QLineEdit *leAcreAns = new QLineEdit();
    13. pbAcreCalc = new QPushButton("&Convert",this);
    14.  
    15. QComboBox *cbAcreF = new QComboBox;
    16. cbAcreF->addItem(tr("Acres"));
    17. cbAcreF->addItem(tr("Sq. Chain, Gunters"));
    18. cbAcreF->addItem(tr("Rods"));
    19. cbAcreF->addItem(tr("Sq. Links, Gunters"));
    20. cbAcreF->addItem(tr("Hectare"));
    21.  
    22. QComboBox *cbAcreT = new QComboBox;
    23. cbAcreT->addItem(tr("Acres"));
    24. cbAcreT->addItem(tr("Sq. Chain, Gunters"));
    25. cbAcreT->addItem(tr("Rods"));
    26. cbAcreT->addItem(tr("Sq. Links, Gunters"));
    27. cbAcreT->addItem(tr("Hectare"));
    28.  
    29. QGridLayout *mLayout = new QGridLayout;
    30. mLayout->addWidget(lblConvertFrom,0,0);
    31. mLayout->addWidget(leConvFrom,0,1);
    32. mLayout->addWidget(cbAcreF,0,2);
    33. mLayout->addWidget(lblConvertTo,1,0);
    34. mLayout->addWidget(leConvTo,1,1);
    35. mLayout->addWidget(cbAcreT,1,2);
    36. mLayout->addWidget(pbAcreCalc, 2,0);
    37. mLayout->addWidget(lblAcreConv, 2,1);
    38. mLayout->addWidget(leAcreAns, 2,2);
    39. setLayout(mLayout);
    40. }
    To copy to clipboard, switch view to plain text mode 

    It compiles with no warnings and runs with no 'object not found' indicators in the run window. All the funtionality relating to tabs, selecting items in combo boxes, etc, work, just nothing happens when the button is pushed from inside the tabbed page. Basically, the program will be a unit conversion program, which allows the user to input one value, and have the conversion done for them. The button is used to take the user input and return a value to them. The button passes back the message to the dialog, which then calls the applicable conversion function to return the converted number back to the user, and show the user the converted number within the tab page.

    Can all this be done within the tab page, that is, get the user input, convert it, then show it back to the user, once the user presses the convert button? Or does the main dialog need to handle the conversion, like I'm trying too now?

    I'm new to Qt so not 100% sure of signals/slots, but how do you pass the signal from the tab page to the dialog app?

    Thanks!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Newbie Help - Signals/Slots between dialog and widgets

    This is a C++ issue. acrTb, created at line 30 in your dialog constructor listing above ceases to exist at the end of the constructor and this severs any connections made to/from it. You need to allocate it on the heap. It seems you do that at line 7, and it is that instance that you should connect.

  3. #3
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Newbie Help - Signals/Slots between dialog and widgets

    Yes, I'm beginning to see that myself, just not sure how to reconcile the code to clean up the mess I've made.....

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Newbie Help - Signals/Slots between dialog and widgets

    Quote Originally Posted by progman View Post
    Can all this be done within the tab page, ...
    Yes. Put the slot in your AcreTab class and the connect statement in the AcreTab constructor.

    Question: If you're converting from one unit of measure to another why three QLineEdits?

  5. The following user says thank you to norobro for this useful post:

    progman (21st March 2011)

  6. #5
    Join Date
    Dec 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Newbie Help - Signals/Slots between dialog and widgets

    Quote Originally Posted by norobro View Post
    Yes. Put the slot in your AcreTab class and the connect statement in the AcreTab constructor.

    Question: If you're converting from one unit of measure to another why three QLineEdits?
    Yes, I realized that too after I posted and it has been fixed....


    Added after 13 minutes:


    Quote Originally Posted by norobro View Post
    Yes. Put the slot in your AcreTab class and the connect statement in the AcreTab constructor.

    Question: If you're converting from one unit of measure to another why three QLineEdits?
    Putting everything into the AcreTab worked perfectly! Thanks!
    Last edited by progman; 21st March 2011 at 03:38.

Similar Threads

  1. Replies: 2
    Last Post: 6th July 2010, 02:08
  2. Replies: 0
    Last Post: 17th April 2010, 17:13
  3. Dynamic widgets adding and connecting signals& slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 22nd May 2009, 12:36
  4. custom signals vs newbie
    By Raccoon29 in forum Newbie
    Replies: 6
    Last Post: 21st April 2008, 16:29
  5. regarding signals/slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 09:32

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
  •  
Qt is a trademark of The Qt Company.