Results 1 to 3 of 3

Thread: Determine which button was clicked on a different window

  1. #1
    Join Date
    Jan 2017
    Qt products

    Default Determine which button was clicked on a different window

    I have an app that has a main window and four sub windows. Let's call the sub windows/modules, A, B, C and D. There are three QPushbuttons each in windows A, B and C (9 total). Each of these nine buttons, when clicked, call the same report function in window D. Depending on which WINDOW sent the click, I want to change some column headings in the report function in window D. I don't really care which of the nine BUTTONS was clicked. It's like I need an "If statement" in the function in window D to determine which Window sent the click. If determining which Window sent the click is not possible, I can still do what I need if I know which BUTTON was clicked.

    I've read about Sender() but do not know if this is what I need nor do I know how to code it. Nor do I know C so its hard to follow examples.

    Sample code for 3 buttons in one of the windows:
    Qt Code:
    1. self.ui.WtgAvgPrintBtn.clicked.connect(self.viewstocks)
    2. self.ui.WtgAvgPrintBtn_2.clicked.connect(self.viewstocks)
    3. self.ui.WtgAvgPrintBtn_3.clicked.connect(self.viewstocks)
    To copy to clipboard, switch view to plain text mode 

    The viewstocks function calls a report function in another window (window D in my example, but it's really called reportsWindow)
    Qt Code:
    1. def viewstocks(self):
    2. self.reportsWindow.byassetclass()
    To copy to clipboard, switch view to plain text mode 

    My questions:
    1. What code do I use in widows A, B and C to designate which Window or button was clicked
    2. What code in window D do I use to determine which Window or button was clicked,. ie. the "If" statement
    3. If I have to go the route of determining which BUTTON was clicked, does it matter if the three button object names in window A are the same as the object names in B and C?

  2. #2
    Join Date
    Jan 2017
    Qt products

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

    RESOLVED: I have decided to simply add two functions (they are small) in the report module -- now there is a unique report for each of the three windows. thanks to all for looking.

  3. #3
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 856 Times in 843 Posts
    Qt products

    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
    3. signals:
    4. void reportSelected( int reportId );
    6. protected slots:
    7. void onButtonClicked();
    9. // ButtonWindow.cpp
    11. ButtonWindow::ButtonWindow( QWidget * parent )
    12. : // base class constructor
    13. {
    14. ui.setupUi( this );
    16. connect( ui.button, &QPushButton::clicked, this, &ButtonWindow::onButtonClicked );
    17. }
    19. void ButtonWindow::onButtonClicked()
    20. {
    21. emit reportSelected( Report1 ); // "Report1" is a value from an enum, for example
    22. }
    24. // ReportWindow.h
    26. public slots:
    27. void onReportSelected( int reportId );
    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;
    38. // ...
    39. }
    40. }
    42. // MainWindow.cpp
    44. MainWindow::MainWindow( QWidget * parent )
    45. : QMainWindow( parent )
    46. {
    47. ui.setupUi( this );
    49. // create report window
    50. ReportWindow * pRW = new ReportWindow( this );
    52. // create button windows
    53. ButtonWindow * pBW = new ButtonWindow( this );
    55. // connect button window to report window
    56. connect( pBW, &ButtonWindow::reportSelected, pRW, &ReportWindow::onReportSelected );
    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.

  4. 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, 23:55
  2. Replies: 6
    Last Post: 9th November 2011, 05:31
  3. Replies: 2
    Last Post: 5th May 2011, 07:53
  4. Replies: 3
    Last Post: 23rd December 2010, 07:55
  5. Replies: 2
    Last Post: 3rd December 2010, 06:52

Tags for this Thread


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.