PDA

View Full Version : Slot - Signal with parameter



Lodhart
9th December 2009, 11:05
Hi, can you help me, please...

i have..


for(x) ( QPushButton * pb = new QPushButton; ... )

x - is 5x or 15x, ... but when i click on button, how i can know which button was pressed ?..
some example will help me..

squidge
9th December 2009, 11:18
QSignalMapper:



for (...) {
QPushButton *button = new QPushButton(...);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(...);
}

connect(signalMapper, SIGNAL(mapped(argtype)), this, SIGNAL(clicked(argtype)));


argtype can be int, QString, anything supported by QSignalMapper

Lodhart
9th December 2009, 11:31
thank you for quick reply ;-)

rajesh
9th December 2009, 11:35
Use Sender()
eg: Button *clickedButton = qobject_cast<Button *>(sender());


void Calculator::unaryOperatorClicked()
{
Button *clickedButton = qobject_cast<Button *>(sender());
QString clickedOperator = clickedButton->text();
double operand = display->text().toDouble();
double result = 0.0;

if (clickedOperator == tr("Sqrt")) {
if (operand < 0.0) {
abortOperation();
return;
}
result = sqrt(operand);
} else if (clickedOperator == tr("x\262")) {
result = pow(operand, 2.0);
} else if (clickedOperator == tr("1/x")) {
if (operand == 0.0) {
abortOperation();
return;
}
result = 1.0 / operand;
}
display->setText(QString::number(result));
waitingForOperand = true;
}

squidge
9th December 2009, 13:21
sender() violates the object-oriented principle of modularity, which is why you should not use it unless there's no alternative (and in this case, there is).

In your example, you could remove 'clickedButton' from your code completely, and have 'clickedOperator' passed to it as a parameter, which is much more readable.

Lodhart
11th December 2009, 11:58
Hi a have a new problem...
when i want change properties of created button, program go to run-time error.


void create () (
for (...) QPushButton * b = new QPushButton;
Struct.b[Struct.top++]=b;
)

theStruct is declare in class MainWindow in public. And in othe fun.:


Struct.b[..]->setText("change");

Tanuki-no Torigava
14th December 2009, 08:46
Hi, look at the calc example bundled with Qt in examples. Looks like your case.

Cheers,
-- tanuki

pipapongo
10th April 2013, 09:54
thanks again but i dont get it just yet...

all the widgets have to know about the myData Object in MainWindow. so is it better to solve this by Singleton or do i give all the widgets a reference to my Data in the constructor, Or is there another way how it should be done?



class myMessage : public QObject
{
Q_OBJECT
public:
myMessage(QObject *parent = 0,myData*);
public slots:
void showMesssage();
void showMesssage(QString *s);
void newMeesageText(QString *s);
private:
QMessageBox *message;

};
//and in MainWIndow i make:
myData * data;
myMessage *mess(this,data);

in that way all the widgets have the pointer to the data in themselve and i i update it in another widget the changes are accessable

I hope you get my question

thank

Santosh Reddy
10th April 2013, 10:08
Post does not make any sense in this thread