PDA

View Full Version : how to conver from 'QObject* const' to 'QLabel'?



kenchan
26th November 2012, 14:22
Hi All,
I'm puzzled about use QStackedwidget.
i have a stackedwidget, and have four labels in one page.
i layout the page by QSplitter. see the below code:


for(int i=0, i<stackedwidget->widget(1)->children().count(); i++)
{
QSplitter *mainSplitter = new QSplitter(Qt::Horizontal, ui->stackedwidget->widget(1));
QSplitter *leftSplitter = new QSplitter(Qt::vertical, mainSplitter);
QSplitter *rightSplitter=new QSplitter(Qt::vertical, mainSplitter);

QLabel *label1 = (QLabel*)(ui->stackedwidget->widget(1)->children().at(0));
QLabel *label2 = (QLabel*)(ui->stackedwidget->widget(1)->children().at(1);
QLabel *label3 = (QLabel*)(ui->stackedwidget->widget(1)->children().at(2));
QLabel *label4 = (QLabel*)(ui->stackedwidget->widget(1)->children().at(3));

leftSplitter->addWidget(lable1);
leftSplitter->addWidget(lable2);
rightSplitter->addWidget(lable3);
rightSplitter->addWidget(lable4);

leftSplitter->setOpaqueResize(false);
rightSplitter->setOpaqueResize(false);
mainSplitter->setStretchFactor(1, 1);
mainSplitter->show();

QGridLayout *layout = new QGridLayout( ui->stackedwidget->widget(1));
layout->addWidget(mainSplitter);
}


it will allerted dialog about "Runtime Error ! This application has requested, the Runtime to terminate it in an unusual way.Please contact the application's support team for morre information".

if doesn't conver, see as bellow:
QLabel *label1 = ui->stackedwidget->widget(1)->children().at(0);

error: invalid conversion from 'QObject* const' to 'QLabel'.

how to conver form from 'QObject* const' to 'QLabel' or anther method to deal with it?

Please give me advice.

Thanks & Best regards.

ken

Lesiok
26th November 2012, 14:28
Something like this :
QLabel *label1 = qobject_cast<QLabel*>(ui->stackedwidget->widget(1)->children().at(0)); and read this (http://qt-project.org/doc/qt-4.8/metaobjects.html)

kenchan
26th November 2012, 14:38
Lesiok ,
i had already used this method ,it also allerted dialog about "Runtime Error ! This application has requested, the Runtime to terminate it in an unusual way.Please contact the application's support team for morre information".

thanks.

amleto
26th November 2012, 14:42
error: invalid conversion from 'QObject* const' to 'QLabel'.

That is a compile time error, not a runtime error. So which is your problem?

It looks like your problem is that what you THINK is a QLabel* is not actually. Have you used your debugger? No? thought not. Go and do so.


QLabel *label1 = (QLabel*)(ui->stackedwidget->widget(1)->children().at(0)); // what if this isnt actually a label??

label1->text(); // label1 isnt a label => crash!

kenchan
26th November 2012, 15:00
Hi amleto,

ui->stackedwidget->widget(1)->children().at(0)->objectName() is label1's objectName . because stackedwidget have several pages, and one page have several filters. so i want to use "for" or "while" to layout.

i use debugger, but have something wrong about debugger, i'm newer, Please give some advice .

thanks.

amleto
26th November 2012, 15:12
you know that you cant 'share' those labels on different stack levels? In each iteration of your loop, you use the exact same label, but there is only one - so you will only see that label on one stack level!

kenchan
27th November 2012, 01:26
Hi amleto and lesiok ,

i have handled it . thank u very much.

BR.

ken