PDA

View Full Version : connect mainwindow with self-made widgets



pinkiP
9th July 2012, 13:19
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


void MainWindow::on_pushButton_clicked()
{
ui->pushButton->setEnabled(false);
QPushButton *button = this->findChild<QPushButton*>("pushButton1");
button->setEnabled(false);
//connect(&ui->widget, SLOT(setFocus()),&ui->pushButton,SIGNAL(setEnabled()));
}

Form1


void Form1::on_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()));
}


I'm using Qt Creator 2.1.0 based on Qt 4.7.1

high_flyer
9th July 2012, 13:47
your connect() statement is wrong it should be:


connect(ui->widget, SLOT(setFocus()),ui->pushButton,SIGNAL(setEnabled()));

You gave the address of the widget pointer, instead the widget pointer it self.

pinkiP
9th July 2012, 14:27
thx, for this fast answer :).

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

high_flyer
9th July 2012, 14:50
Do you really have a 'widget' member in 'ui'?

pinkiP
9th July 2012, 15:00
Yes, I do. I renamed it and get the same error message. The name shouldn't be the problem.

high_flyer
9th July 2012, 15:03
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.

pinkiP
9th July 2012, 15:14
:-) 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::on_pushButton1_clicked()'". And yes, I renamed an re-builded the project.

high_flyer
9th July 2012, 15:21
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.

d_stranz
9th July 2012, 19:01
void Form1::on_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?

pinkiP
10th July 2012, 08:24
@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.

high_flyer
10th July 2012, 09:20
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?

pinkiP
10th July 2012, 10:16
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:

Form1::Form1(QWidget *parent) :QWidget(parent),ui(new Ui::Form1)
{
ui->setupUi(this);
connect(ui->pushButton1,SIGNAL(clicked()),this,SIGNAL(wantToSe tEnabled()));
}

In form1.h:

signals:
void wantToSetEnabled();

In mainwindow.cpp:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->winwin,SIGNAL(wantToSetEnabled()),ui->pushButton,SLOT(click()));
}

thx, for your time and your patience.