Results 1 to 7 of 7

Thread: how to conver from 'QObject* const' to 'QLabel'?

  1. #1
    Join Date
    Oct 2012
    Posts
    35
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default how to conver from 'QObject* const' to 'QLabel'?

    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

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to conver from 'QObject* const' to 'QLabel'?

    Something like this :
    Qt Code:
    1. QLabel *label1 = qobject_cast<QLabel*>(ui->stackedwidget->widget(1)->children().at(0));
    To copy to clipboard, switch view to plain text mode 
    and read this

  3. #3
    Join Date
    Oct 2012
    Posts
    35
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to conver from 'QObject* const' to 'QLabel'?

    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.
    Last edited by kenchan; 26th November 2012 at 14:39. Reason: updated contents

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to conver from 'QObject* const' to 'QLabel'?

    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!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Oct 2012
    Posts
    35
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to conver from 'QObject* const' to 'QLabel'?

    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.
    Last edited by kenchan; 26th November 2012 at 15:02. Reason: updated contents

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to conver from 'QObject* const' to 'QLabel'?

    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!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Oct 2012
    Posts
    35
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to conver from 'QObject* const' to 'QLabel'?

    Hi amleto and lesiok ,

    i have handled it . thank u very much.

    BR.

    ken

Similar Threads

  1. Replies: 2
    Last Post: 24th December 2015, 13:33
  2. Conver QtcpSocket to QSslSocket
    By k.qasempour in forum Newbie
    Replies: 1
    Last Post: 6th November 2012, 21:11
  3. how to conver QString to const char*
    By gauravg in forum Qt Programming
    Replies: 16
    Last Post: 15th April 2011, 14:03
  4. What's Up With "const QObject &"
    By wswartzendruber in forum Newbie
    Replies: 7
    Last Post: 15th October 2009, 11:53
  5. Replies: 1
    Last Post: 29th September 2009, 19:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.