Results 1 to 3 of 3

Thread: Determine which button was clicked on a different window

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Determine which button was clicked on a different window

    Back to your original post - there is a better way to abstract this than to require your report window to decipher which button in some other, completely unrelated window was the one that was clicked. What happens if you want to add to your UI the ability to change reports based on a menu action or a combobox selection? Your logic to change reports based on button clicks falls apart completely because there is no button.

    So what I would do is this:

    1 - in your report window, implement a slot that takes as an argument some type of ID - an integer, enum value, string, whatever - that uniquely identifies the report you want to produce

    2 - in the windows that hold your buttons, implement slots for each of the button clicked() signals.

    3 - in those same windows, implement signals the send an ID value when emitted. In the button click handling slots, emit the signal with the appropriate id value

    4 - connect the signals in the button windows to the slot in the report window.

    Something like this:

    Qt Code:
    1. // ButtonWindow.h
    2.  
    3. signals:
    4. void reportSelected( int reportId );
    5.  
    6. protected slots:
    7. void onButtonClicked();
    8.  
    9. // ButtonWindow.cpp
    10.  
    11. ButtonWindow::ButtonWindow( QWidget * parent )
    12. : // base class constructor
    13. {
    14. ui.setupUi( this );
    15.  
    16. connect( ui.button, &QPushButton::clicked, this, &ButtonWindow::onButtonClicked );
    17. }
    18.  
    19. void ButtonWindow::onButtonClicked()
    20. {
    21. emit reportSelected( Report1 ); // "Report1" is a value from an enum, for example
    22. }
    23.  
    24. // ReportWindow.h
    25.  
    26. public slots:
    27. void onReportSelected( int reportId );
    28.  
    29. // ReportWindow.cpp
    30. void ReportWindow::onReportSelected( int reportId )
    31. {
    32. switch reportId:
    33. {
    34. case Report1:
    35. // format a Report1 type report...
    36. break;
    37.  
    38. // ...
    39. }
    40. }
    41.  
    42. // MainWindow.cpp
    43.  
    44. MainWindow::MainWindow( QWidget * parent )
    45. : QMainWindow( parent )
    46. {
    47. ui.setupUi( this );
    48.  
    49. // create report window
    50. ReportWindow * pRW = new ReportWindow( this );
    51.  
    52. // create button windows
    53. ButtonWindow * pBW = new ButtonWindow( this );
    54.  
    55. // connect button window to report window
    56. connect( pBW, &ButtonWindow::reportSelected, pRW, &ReportWindow::onReportSelected );
    57.  
    58. }
    To copy to clipboard, switch view to plain text mode 

    The nice thing about this, even though it uses more signal and slot overhead, is that it completely decouples buttons from reports. The report window doesn't know how the signal to change the report type got generated, all it knows is that it should change the type to whatever was requested. Likewise, the buttons don't know that they are changing a report format, they just know they might be telling something, somewhere, that a new report ID has been chosen.

    So this mean you can send a signal with a report ID from anywhere in your UI - a menu, combobox, push button, radio button, whatever - so long as the signal that the control emits is connected to a slot that in turn emits a signal with a report ID. Connect that signal to the report window's slot, and you have added a new capability to your program with no change in the architecture.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    dennisvz (4th December 2019)

Similar Threads

  1. Replies: 3
    Last Post: 9th September 2014, 22:55
  2. Replies: 6
    Last Post: 9th November 2011, 04:31
  3. Replies: 2
    Last Post: 5th May 2011, 06:53
  4. Replies: 3
    Last Post: 23rd December 2010, 06:55
  5. Replies: 2
    Last Post: 3rd December 2010, 05:52

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.