QMessageBox too small



  • Hello,

    I have a problem with the size of my QMessageBox.
    [code] QMessageBox msg(this);
    msg.setIconPixmap(QPixmap(":/error.svg"));
    msg.setText("No images loaded");
    msg.setInformativeText("There are no images in the list. Please create or load a project");
    msg.setStandardButtons(QMessageBox::Ok);
    msg.setDefaultButton(QMessageBox::Ok);
    msg.setFixedWidth(500);
    msg.exec();[/code]

    It is every time to small in the width and I don't know why. My parent widget is a QWidget with a minimum size.
    The function "msg.setFixedWidth(300);" has no effect, I can also change it to 800 and it has always a width of approx. 280px.

    I can also make a new class which inherits QDialog and write it by my own, but I would prefere to use QMessageBox.
    Can anybody tell me, why the size of my QMessageBox is so small?

    I am programming in Linux Mint 13 (MATE) with Qt4.8.1

    Thank you!



  • Aren't you tilting at windmills: fighting a problem that doesn't need fixing? That the dialog is functional is sufficient. Is "too small" a style issue, or a real problem to the user?

    I would guess that a QMessageBox, designed to be convenient to use and cross-platform, is smart enough to size itself so as to minimize hiding of other windows and so forth. So it seems reasonable that it might have overridden the QWidget.setFixedWidth() method, although the documents don't state that.



  • This sounds strange. Although I did not find similar issues reported for Qt 4.8 at "jira":https://bugreports.qt-project.org it might be a bug.

    Anyway I found out "this proposition for a work around at qtcenter.org that you may try":http://www.qtcentre.org/threads/22298-QMessageBox-Controlling-the-width?p=113348#post113348:

    @
    QMessageBox msg(this);
    msg.setIconPixmap(QPixmap(":/error.svg"));
    msg.setText("No images loaded");
    msg.setInformativeText("There are no images in the list. Please create or load a project");
    msg.setStandardButtons(QMessageBox::Ok);
    msg.setDefaultButton(QMessageBox::Ok);

    QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
    QGridLayout* layout = (QGridLayout*)msg.layout();
    layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
    msg.exec();
    @



  • Thank you, it seems to work with your work around.
    But I am still wondering, why it doesn't resize by itself.

    I have also another similar program, where the QMessageBox works well and I tried to find out their difference with no result. It's a little bit strange.

    @bootchk: I am only wondering, why the size of the window is too small in the width. Maybe somebody knows the problem. I thought also, that it could be something related to the parent widget, but also without that, the window is to small in the width. And inanother similar program, it works well.



  • [quote author="Serenity" date="1360759711"]Thank you, it seems to work with your work around.
    But I am still wondering, why it doesn't resize by itself.
    [/quote]

    I have no idea with setFixedWidth didn't work as expected too. In fact the work around is not mine. As I said I found it at "an old thread at Qt Centre":http://www.qtcentre.org/threads/22298-QMessageBox-Controlling-the-width?p=113348#post113348



  • After 3 years, it is still not fixed


  • Qt Champions 2016

    @Wonson
    well maybe it was never reported ?



  • Has anybody found a solutions to this? I tried http://www.qtcentre.org/threads/22298-QMessageBox-Controlling-the-width?p=113348#post113348 to no avail. Is seems my QMessageBox won't expand beyond it's internal maximum width.


  • Qt Champions 2016

    @DougyDrumz
    hi this does work for me

    #include <QMessageBox>
    #include <QGridLayout>
    #include <QSpacerItem>
    
    void MainWindow::on_pushButton_released()
    {
    
        QMessageBox msg;
        msg.setIconPixmap(QPixmap(":/error.svg"));
        msg.setText("No images loaded");
        msg.setInformativeText("There are no images in the list. Please create or load a project");
        msg.setStandardButtons(QMessageBox::Ok);
        msg.setDefaultButton(QMessageBox::Ok);
        QSpacerItem* horizontalSpacer = new QSpacerItem(800, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
        QGridLayout* layout = (QGridLayout*)msg.layout();
        layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
        msg.exec();
    }
    


  • This doesn't work for me. One thing I've noticed: You're opening this QMessageBox in a QMainWindow. My QMessageBox is opened in main. This is because I open the QMessageBox using an executable from the command line. It's not tied to any app. Could that be an issue? If it helps, I'm running Qt 4.6.2 on RHEL 6 machine.


  • Qt Champions 2016

    @DougyDrumz
    win 7, qt 5.5
    well im not using "this" from mainwindow ,
    so should be same as in main.cpp
    Let me try.
    update:
    also works from main.

    Maybe its diff between win and linux. But looking at code it seems that
    its build with qt widgets so should function the same.


  • Qt Champions 2016

    hi
    could you try for test to run this

    #include <QList>
    void Pop() {
     QMessageBox msg;
     msg.setIconPixmap(QPixmap(":/error.svg"));
     msg.setText("No images loaded");
     msg.setInformativeText("There are no images in the list. Please create or load a project");
     msg.setStandardButtons(QMessageBox::Ok);
     msg.setDefaultButton(QMessageBox::Ok);
     QList<QObject*> widgets = msg.findChildren<QObject*>();
     for (int var = 0; var < widgets.size(); ++var) {
       qDebug() << widgets[var]->objectName() << widgets[var]->metaObject()->className();
     }
     msg.exec();
    }
    

    its lists all objects.
    my list is
    "qt_msgboxex_icon_label" QLabel
    "qt_msgbox_label" QLabel
    "qt_msgbox_buttonbox" QDialogButtonBox
    "" QHBoxLayout
    "" QPushButton
    "" QGridLayout
    "qt_msgbox_informativelabel" QLabel
    just too see if inner layout is different for you.



  • Same objects, different order:

    "" QGridLayout
    "qt_msgboxex_icon_label" QLabel
    "qt_msgbox_label" QLabel
    "qt_msgbox_buttonbox" QDialogButtonBox
    "" QHBoxLayout
    "" QPushButton
    "qt_msgbox_informativelabel" QLabel


  • Qt Champions 2016

    @DougyDrumz
    Ok, nothing exciting there.
    looking at the code in qmessagebox.cpp i see nothing that should it do different
    on linux.
    Have you looked in your version ?
    seems like it's a lost cause as most of the stuff is in private.


Log in to reply
 

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