PDA

View Full Version : Passing QLabel into a function as a parameter



mynahz
9th July 2008, 08:18
I am trying to 'blink' pictures by aternating between 2 QLabels. And since I will have to 'blink' many icons, I was think whether I am able to pass in QLabel like this:

void Mainframe::blinkLabel1_2(QLabel* labelOne, QLabel* labelTwo)
{
static bool on = false;

if(on)
{
labelOne->show();
labelTwo->hide();
on = false;
}
else
{
labelTwo->show();
labelOne->hide();
on = true;
}
}

but I face the following errors

src\mainframe.cpp:42: error: no matching function for call to `Mainframe::blinkLabel1_2(QLabel*&, QLabel*&)'
src\mainframe.h:15: note: candidates are: void Mainframe::blinkLabel1_2(QLabel, QLabel)
src\mainframe.cpp: In member function `void Mainframe::on_canbus_RevBoolData(bool)':
src\mainframe.cpp:85: error: no matching function for call to `Mainframe::blinkLabel1_2(QLabel*&, QLabel*&)'
src\mainframe.h:15: note: candidates are: void Mainframe::blinkLabel1_2(QLabel, QLabel)
src\mainframe.cpp: At global scope:
src\mainframe.cpp:92: error: prototype for `void Mainframe::blinkLabel1_2(QLabel&, QLabel&)' does not match any in class `Mainframe'
src\mainframe.h:15: error: candidate is: void Mainframe::blinkLabel1_2(QLabel, QLabel)
src\mainframe.cpp: In member function `void Mainframe::blinkLabel1_2(QLabel&, QLabel&)':
src\mainframe.cpp:97: error: base operand of `->' has non-pointer type `QLabel'
src\mainframe.cpp:98: error: base operand of `->' has non-pointer type `QLabel'
src\mainframe.cpp:103: error: base operand of `->' has non-pointer type `QLabel'
src\mainframe.cpp:104: error: base operand of `->' has non-pointer type `QLabel'
mingw32-make[1]: Leaving directory `C:/HmiProject/imitDemo1'
mingw32-make[1]: *** [build\host\mainframe.o] Error 1
mingw32-make: *** [release] Error 2

I tried changing the statement to
void Mainframe::blinkLabel1_2(QLabel& labelOne, QLabel& labelTwo)
void Mainframe::blinkLabel1_2(QLabel *labelOne, QLabel *labelTwo)
void Mainframe::blinkLabel1_2(QLabel &labelOne, QLabel &labelTwo)
but it still couldn't work.

Appreciate if someone enlightens me on this. :)

wysota
9th July 2008, 08:24
First of all it would be simpler if you exchanged pixmaps and not whole labels.

Second of all, you have to show us how you call your function. It should be something like:

QLabel *l1, *l2;
//...
blinkLabel1_2(l1, l2);
where

void ...::blinkLabel1_2(QLabel*, QLabel*);

mynahz
10th July 2008, 07:38
Hi wysota!
first of all, I use whole labels because of the convinence of setting the picture inside Qt designer. :o

second, here's my code.
where i call the function blinkLabel if I don't pass parameters.

void Mainframe::on_canbus_RevBoolData(bool temp)
{
// TODO
label->hide();
label_2->hide();

if(temp)
{
label_2->show();
label->hide();
}
else
{
/**********************************************/
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(blinkLabel1_2()));
timer->start(1000);
this->blinkLabel1_2();
/**********************************************/
}

}

void Mainframe::blinkLabel1_2()
{
static bool on = false;

if(on)
{
label->show();
label_2->hide();
on = false;
}
else
{
label_2->show();
label->hide();
on = true;
}
}


if I pass in parameters (from what u suggested:))

void Mainframe::on_canbus_RevBoolData(bool temp)
{
// TODO
label->hide();
label_2->hide();

if(temp)
{
label_2->show();
label->hide();
}
else
{
/**********************************************/
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(blinkLabel1_2(label, label_2)));
timer->start(1000);
this->blinkLabel1_2(label, label_2);
/**********************************************/
}

}

void Mainframe::blinkLabel1_2(QLabel* l1, QLabel* l2)
{
static bool on = false;

if(on)
{
l1->show();
l2->hide();
on = false;
}
else
{
l2->show();
l1->hide();
on = true;
}
}

no errors encountered, but I have no idea now why it doesn't blink... :(

Thanks for yr reply wysota! :)

jogeshwarakundi
17th July 2008, 07:43
Do you find any error printed at runtime regarding this connection? a Slot cannot have more parameters than a Signal. You have to figure out another way of passing the label1 and label2 to the slot. See the Signal Slot documentation for more information.