Results 1 to 5 of 5

Thread: Access widget inside the QMainWindow centralwidget

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Access widget inside the QMainWindow centralwidget

    Hi
    I'm trying to connect a button that is inside a widget(myCentralWidget). This widget is set as the QMainWindow central widget.
    This is an example:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. widget = new myCentralWidget(this);
    10. setCentralWidget(widget);
    11. // ...
    12. }
    To copy to clipboard, switch view to plain text mode 

    All classes are created using Qt Designer Form class from Qt Creator: MainWindow and myCentralWidget.
    How do i connect a button that is inside the ui of the myCentralWidget to make something happen in the qmainwindow?
    Thanks

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

    Default Re: Access widget inside the QMainWindow centralwidget

    The cleanest way is to forward the signal in myCentralWidget so that the myCentralWidget class has a signal you can connect to in MainWindow.

    So you declare a signal in myCentralWidget
    Qt Code:
    1. class myCentralWidget : public QWidget
    2. {
    3. Q_OBjECT
    4. public:
    5. // ...
    6.  
    7. signals:
    8. void someButtonClicked(); // or use a name that indicates that the user wants to do
    9. };
    To copy to clipboard, switch view to plain text mode 
    and forward the button's signal, e.g. in the constructor
    Qt Code:
    1. connect( ui->someButton, SIGNAL(clicked()), this, SIGNAL(someButtonClicked()) );
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    graciano (23rd January 2014)

  4. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access widget inside the QMainWindow centralwidget

    Still don't get it ... i will take a better look at this: https://qt-project.org/doc/qt-5/signalsandslots.html
    Never tried to forward a signal before

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access widget inside the QMainWindow centralwidget

    When the button is clicked, it emits its clicked() signal.
    Due to the signal/signal connect the container widget's someButtonClicked() signal is now also emitted.

    In MainWindow you have the object of the container widget, so you can connect its someButtonClicked() signal just like if it where the button.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    graciano (23rd January 2014)

  7. #5
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access widget inside the QMainWindow centralwidget

    I get it ... and it is simple after all.
    Thanks for the patience anda_skoa.
    I attach example: question.zip

Similar Threads

  1. Cant get QMainWindow().centralwidget....
    By tonnot in forum Newbie
    Replies: 2
    Last Post: 8th December 2011, 01:49
  2. Replies: 1
    Last Post: 12th October 2010, 05:39
  3. QMainWindow inside another widget
    By DanFessler in forum Newbie
    Replies: 4
    Last Post: 23rd August 2010, 22:58
  4. Replies: 4
    Last Post: 4th December 2009, 11:04
  5. QMainWindow -> centralWidget size
    By lalesculiviu in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 10:40

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.