简体   繁体   中英

QDialog expanding base on the QLabel content

I'd like to show and then close a dialog after 5 seconds. The dialog needs to be automatically resized (horizontally and vertically) based on the content of a label. Here is my code:

#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>

void notify (int intTime=1000)
{
    QDialog notify;
    notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    notify.setWindowFlag(Qt::FramelessWindowHint);
    QLabel *lbl = new QLabel(&notify);
    lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
    QApplication::processEvents();
    notify.adjustSize();
    QTimer::singleShot(intTime, &notify, SLOT(close()));
    notify.exec();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    notify(5000);
    exit(0);
//  return a.exec();
}

It does not not expand the dialog based on the label size. Here is how it looks:

在此处输入图片说明

How can I fix it? (Please also let me know if there is better way of doing this.)

I am using Qt5 in Linux.

Since you have not used a QLayout the QLabel will be displayed as large as you can, a possible request is to change the size of QDialog to the recommended size of QLabel with sizeHint() :

#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>

void notify (int intTime=1000)
{
    QDialog notify;
    notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    notify.setWindowFlag(Qt::FramelessWindowHint);
    QLabel *lbl = new QLabel(&notify);
    lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
    QApplication::processEvents();
    notify.resize(lbl->sizeHint());
    QTimer::singleShot(intTime, &notify, SLOT(close()));
    notify.exec();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    notify(5000);
    exit(0);
//  return a.exec();
}

The other possible solution is to use a QLayout :

#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>

void notify (int intTime=1000)
{
    QDialog notify;
    QVBoxLayout *lay = new QVBoxLayout(&notify);
    //notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    notify.setWindowFlag(Qt::FramelessWindowHint);
    QLabel *lbl = new QLabel;
    lay->addWidget(lbl);
    lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
    QApplication::processEvents();
    QTimer::singleShot(intTime, &notify, SLOT(close()));
    notify.exec();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    notify(5000);
    exit(0);
//  return a.exec();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM