Results 1 to 12 of 12

Thread: connect mainwindow with self-made widgets

  1. #1
    Join Date
    Jun 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default connect mainwindow with self-made widgets

    Hi,

    I created a complex application with the qt creator (Below: I created a minimal example). Now I have a main window with some widgets (e.g. Form1) like buttons, which are always visible and I have some self-made widgets, which are promoted to a widget container. In one of this self-made widgets, there is a button (named pushButton1) which is related to a button (named: pushButton) on the main window. So I like to click one of the buttons and the clicked button and the other button appear enabled. The direction pushButton clicked and pushButton and pushButton1 are set enabled works. The other direction doesn't work. I don't get an error massage, if I click on pushButton1 itself appears enabled, but not the other pushbutton.
    As you can see, I also tried it with signals and slots, but here I get the following error massage: "no matching function for call to 'MainWindow::connect(Form1 **, const char*, QPushButton**,const char*)'". Could anyone help me please?!

    MainWindow
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. ui->pushButton->setEnabled(false);
    4. QPushButton *button = this->findChild<QPushButton*>("pushButton1");
    5. button->setEnabled(false);
    6. //connect(&ui->widget, SLOT(setFocus()),&ui->pushButton,SIGNAL(setEnabled()));
    7. }
    To copy to clipboard, switch view to plain text mode 
    Form1
    Qt Code:
    1. void Form1::on_pushButton1_clicked()
    2. {
    3. ui->pushButton1->setEnabled(false);
    4. MainWindow mainw;
    5. QPushButton *button = mainw.findChild<QPushButton*>("pushButton");
    6. button->setEnabled(false);
    7. //connect(&ui->pushButton1, SLOT(on_pushButton1_clicked()),&Form1 ,SIGNAL(setFocus()));
    8. }
    To copy to clipboard, switch view to plain text mode 

    I'm using Qt Creator 2.1.0 based on Qt 4.7.1

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect mainwindow with self-made widgets

    your connect() statement is wrong it should be:
    Qt Code:
    1. connect(ui->widget, SLOT(setFocus()),ui->pushButton,SIGNAL(setEnabled()));
    To copy to clipboard, switch view to plain text mode 
    You gave the address of the widget pointer, instead the widget pointer it self.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jun 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect mainwindow with self-made widgets

    thx, for this fast answer .

    I did it, but I get the following error massage: "expected primary-expression before ',' token".

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect mainwindow with self-made widgets

    Do you really have a 'widget' member in 'ui'?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jun 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect mainwindow with self-made widgets

    Yes, I do. I renamed it and get the same error message. The name shouldn't be the problem.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect mainwindow with self-made widgets

    Yes, I do. I renamed it and get the same error message. The name shouldn't be the problem.
    Yes it should, and it will.
    Make sure you rebuild your project after changing things in your ui file.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jun 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect mainwindow with self-made widgets

    :-) well, no error message for this connect(...). Now I got the same error message for the other connect(...). The line before the error shows: "In member function 'void Form1:n_pushButton1_clicked()'". And yes, I renamed an re-builded the project.
    Last edited by pinkiP; 9th July 2012 at 16:19.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect mainwindow with self-made widgets

    Try to solve it your self this time, based on the solution I helped you with.
    Read well the error, see what line it is on, and check you code very carefully.
    If you then still don't find what the problem is, ask again, with explanation to what you have done and tried, and why.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,310
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: connect mainwindow with self-made widgets

    void Form1:n_pushButton1_clicked()
    {
    ui->pushButton1->setEnabled(false);
    MainWindow mainw;
    QPushButton *button = mainw.findChild<QPushButton*>("pushButton");
    button->setEnabled(false);
    //connect(&ui->pushButton1, SLOT(on_pushButton1_clicked()),&Form1 ,SIGNAL(setFocus()));
    }
    A hint: Why do you think that creating another MainWindow instance on the stack in this slot is going to do anything useful? Especially because as soon as that slot exits, the new MainWindow and all of its children are going to be deleted, including the QPushButton instance you think you found?

    Another hint: if all you are trying to do with all this "findChild()" stuff is to determine which button sent the signal, then just use the QObject::sender() method inside the slot.

    And yet another hint: Why do you think that passing the address of a class (&Form1) in the connect() call you have commented out has any meaning? Wouldn't it make more sense to pass the address of an instance of the class instead?

    And yet another additional hint: Every time you call "connect" you set up another connection between the signal and slot. That means every time your slot is executed, you add another connection. If the user clicks the button once, now there are two connections. Click it again, you have four, click it again, you have eight, ... Each new click will execute the slot once for each connection, and each execution will add a new connection; when all have finished you have double the number of connections than before the click.

    How many grains of rice on that chess board after even a few clicks?
    Last edited by d_stranz; 9th July 2012 at 20:11.

  10. #10
    Join Date
    Jun 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect mainwindow with self-made widgets

    @d_stranz: Your comment sounds like: What the hell are you doing? and: This is the wrong approach!
    And yet another additional hint: Every time you call "connect" you set up another connection between the signal and slot. That means every time your slot is executed, you add another connection. If the user clicks the button once, now there are two connections. Click it again, you have four, click it again, you have eight, ... Each new click will execute the slot once for each connection, and each execution will add a new connection; when all have finished you have double the number of connections than before the click.
    I set the buttons disable, so clicking on the button shouldn't matter. Respectively, the user should see and understand that clicking on this button doesn't matter. So I don't think that I have this problem.

    @high_flyer: I replaced "Form1" through "this". Now I've no error messages, but also no effects on the buttons.
    I keep trying.

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: connect mainwindow with self-made widgets

    I replaced "Form1" through "this". Now I've no error messages, but also no effects on the buttons.
    I keep trying.
    It seems to me you have no idea what you are doing - and whats worse - why you are doing it.
    Can you explain why did you change from "Form1" to "this"?
    Do you understand what each case actually means?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Jun 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect mainwindow with self-made widgets

    As I understand, Form1 is the class and this is the pointer of the class (object).

    Now solved my problem:
    If I get a signal from the pushbutton1, I send a signal with the form. This signal will be catch from the pushbutton in the mainwindow.

    In form1.cpp:
    Qt Code:
    1. Form1::Form1(QWidget *parent) :QWidget(parent),ui(new Ui::Form1)
    2. {
    3. ui->setupUi(this);
    4. connect(ui->pushButton1,SIGNAL(clicked()),this,SIGNAL(wantToSetEnabled()));
    5. }
    To copy to clipboard, switch view to plain text mode 

    In form1.h:
    Qt Code:
    1. signals:
    2. void wantToSetEnabled();
    To copy to clipboard, switch view to plain text mode 

    In mainwindow.cpp:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. connect(ui->winwin,SIGNAL(wantToSetEnabled()),ui->pushButton,SLOT(click()));
    7. }
    To copy to clipboard, switch view to plain text mode 

    thx, for your time and your patience.

Similar Threads

  1. Replies: 4
    Last Post: 26th August 2010, 15:13
  2. How to put 2 Widgets in a MainWindow ?
    By gen_mass in forum Newbie
    Replies: 13
    Last Post: 1st July 2010, 13:01
  3. Replies: 8
    Last Post: 16th January 2009, 17:10
  4. Child Widgets In MainWindow
    By RY in forum Newbie
    Replies: 3
    Last Post: 4th October 2008, 09:39
  5. How to get correct header with custom made widgets?
    By Arthur in forum Qt Programming
    Replies: 3
    Last Post: 26th April 2007, 18:07

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.