View Full Version : How to connect a slot(QWidget*) with signal(clicked())
revellix
5th August 2011, 12:27
Hey mates.
I want to connect a button's clicked() signal with a custom slot that gets a QWidget as parameter. Afaik it is not possible to connect non-parameter signals with parameter slots.
How can I solve that?
connect(startButton,SIGNAL(clicked()),this,SLOT(ca lculationAlgoStart(subWindowTabWidget)));
any suggestions ?
Jonny174
5th August 2011, 12:35
Reimplement startButton and make your signal. Maybe this help:
class PushButton : public QPushButton
{
Q_OBJECT
public:
PushButton( QWidget *parent = 0 );
virtual ~PushButton() {}
protected:
virtual void click();
signals:
void onClick( QWidget * );
};
PushButton::PushButton( QWidget *parent ):
QPushButton( parent )
{
}
void PushButton::click()
{
emit onClick( this );
}
Lykurg
5th August 2011, 12:43
...or make an custom slot, which calls calculationAlgoStart(subWindowTabWidget).
revellix
5th August 2011, 13:32
...or make an custom slot, which calls calculationAlgoStart(subWindowTabWidget).
no sense. I have to give the custom slot the parameter subWindowTabWidget....
Lykurg
5th August 2011, 14:21
no sense. A lot of sense, if one try to understand...
connect(startButton,SIGNAL(clicked()),this,SLOT(my Slot()));
//...
void XXX:mySlot()
{
calculationAlgoStart(subWindowTabWidget);
}
And if you have more buttons you could use QSignalMapper. But this statement probably also has no sense.
revellix
5th August 2011, 15:13
subWindowTabWidget is a parameter that I give to the constructor... finally I have to tell the slot(), what subWindowTabWidget is. So I have again: mySlot(QTabWidget *).
Yaaa... QSignalMapper... I tried to use the class, but I dont get it. Too newbish for that. If you want to explain it, feel free to do so.
Lykurg
5th August 2011, 15:17
subWindowTabWidget is a parameter that I give to the constructor... finally I have to tell the slot(), what subWindowTabWidget is. So I have again: mySlot(QTabWidget *).Umpf, ok, lets take small steps: Where is your connect statement? Does it look like
MyClass::MyClass(/*...*/, QObject* subWindowTabWidget)
{
// Ui setup (also startButton)
connect(startButton, SIGNAL(clicked()),
this, SLOT(calculationAlgoStart(subWindowTabWidget)));
}?
revellix
5th August 2011, 15:29
Constructor:
UserInput::UserInput(QWidget *parent, int form, QTabWidget *subWindowTabWidget) : QWidget(parent)
{
this->setAttribute(Qt::WA_DeleteOnClose);
this->setLayout(userInputLayout);
this->constructUserInput(form, subWindowTabWidget); ........
}
Constructormethod:
void UserInput::constructUserInput(int constructForm, QTabWidget *)
{
....
connect(startButton,SIGNAL(clicked()),this, SLOT(calculationAlgoStart(QTabWidget *)));
....
}
CalculateAlgorithm...:
void UserInput::calculationAlgoStart(QTabWidget *)
{
....
Lykurg
5th August 2011, 15:37
In that case, why do you need to pass the pointer at all? Make a private member variable and use this in your slot.
class UserInput
{
private:
QTabWidget *m_subWindowTabWidget;
};
UserInput::UserInput(QWidget *parent, int form, QTabWidget *subWindowTabWidget) : QWidget(parent), m_subWindowTabWidget(subWindowTabWidget)
{
this->setAttribute(Qt::WA_DeleteOnClose);
this->setLayout(userInputLayout);
connect(startButton,SIGNAL(clicked()),this, SLOT(calculationAlgoStart()));
}
void UserInput::calculationAlgoStart(QTabWidget *)
{
// simple use m_subWindowTabWidget here
}
revellix
5th August 2011, 16:06
see... this is why I am too newbish. THX mate.
how is this parameter substitution called ? I should read something about that.
Lykurg
5th August 2011, 16:19
It is simple use of private members. Nothing special. If you mean the initialization of members alongside the c-tor, see (constructor) initialization lists.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.