'messageLabel' was not declared in this scope



  • I'm trying to display a success/failure message on QDialog label for 5 secs and the QDialog should close automatically. What I'm having difficulty with is updating the text of the label. Here is the code:

    void Camera::displayAttemptedSaveResult(QString msg)
    {
        AttemptedSaveResultDisplay saveResult;
        saveResult.setModal(true);
        saveResult.setWindowFlags(Qt::CustomizeWindowHint); 
        saveResult.setWindowFlags(Qt::FramelessWindowHint); 
        messageLabel->setText(msg);
        saveResult.exec();
        QTimer::singleShot(5000,&saveResult, SLOT(close()));
    }
    

    messageLabel->setText(msg); produces an error "'messageLabel' was not declared in this scope"

    How can I modify the QDialog label text?


  • Lifetime Qt Champion

    Hi,

    is messageLabel a widget from AttemptedSaveResultDisplay ?



  • @SGaist

    The QDialog and messageLabel were created using Qt Designer and I can't use ui->messageLabel->setText(msg); because AttemptedSaveResultDisplay is a static function? How can I get around this problem?


  • Lifetime Qt Champion

    AttemptedSaveResultDisplay is not a static function but a class. The cleanest way to do that is to add a setter to AttemptedSaveResultDisplay (e.g. setMessage) that will call ui->messageLabel->setText(msg);



  • I'm considering a different solution to my problem but it seems that there is another problem with the code. I intended for the QDialog to pop up for 5 seconds, then closes but this line

    QTimer::singleShot(5000,&saveResult, SLOT(close()));
    

    doesn't seem to get the QDialog to close after 5 seconds. What am I doing wrong?


  • Lifetime Qt Champion

    That's normal, you call it after saveResult.exec(); which is a blocking call.



  • Thank you very much.....didn't pay attention to exec() :)

    I have 3 last questions about the code, hopefully!

    void Camera::displayAttemptedSaveResult(QString msg)
    {
        AttemptedSaveResultDisplay saveResult;
        saveResult.setModal(true);
        saveResult.setWindowFlags(Qt::CustomizeWindowHint); 
        saveResult.setWindowFlags(Qt::FramelessWindowHint); 
    
        QLabel *message = new QLabel(&saveResult);
        message->setStyleSheet("QLabel { font-size:16pt; color:#FF1900;}");
        QHBoxLayout *layout = new QHBoxLayout();
        message->setText(msg);
        message->setAlignment(Qt::AlignHCenter);
        message->setFixedWidth(300);
        layout->addWidget(message);
        saveResult.setLayout(layout);
    
        QTimer::singleShot(5000,&saveResult, SLOT(close()));
        saveResult.exec();
    }
    
    1. I come from java background were object collection is done automatically. Is
    QLabel *message = new QLabel(&saveResult);
    

    the correct way to create the QLabel so it won't cause memory leaks?

    1. Is there a better way than
    message->setFixedWidth(300);
    

    to make sure all text is displayed automatically whether long or short string is used? If I comment it out, a small part of the text is displayed.

    1. The text is displayed in the top right of the dialog even though I intended for it to display centered vertically and horizontally,
     message->setAlignment(Qt::AlignHCenter);
    

    What am I missing?


  • Lifetime Qt Champion

    1. Since you put message in a layout that you set on saveResult, then there no need to pass the parent when constructing message.

    Doesn't AttemptedSaveResultDisplay already have a layout ?

    1. message should resize itself to match the size of the text.

    2. The text might be centered in the label but the label could be at the wrong position.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.