[solved]Strange widget positioning within a QToolBar



  • I have a problem with adding 3 widgets arranged in a QGridLayout.
    It works when I insert this into a QToolbar that I have defined with designer.
    I fails when this QToolbar is defined within the code. @QToolBar* uiToolbar = new QToolBar(mainWindow);

    QWidget *w = new QWidget;
    QGridLayout *gbox = new QGridLayout;

    QComboBox* parametersCB = new QComboBox;
    QLabel* typeLabel = new QLabel("type");
    QLabel* currentLabel = new QLabel(tr("Current parameters:"));

    gbox->addWidget(currentLabel,0,0,1,2);
    gbox->addWidget(parametersCB,1,0);
    gbox->addWidget(typeLabel,1,1);

    w->setLayout(gbox);
    uiToolbar->addWidget(w);
    mainWindow->addToolBar(Qt::TopToolBarArea, uiToolbar);@ I get this !http://i.imgur.com/uDNAY.png!
    And after moving it one widget is on top of another one:!http://i.imgur.com/PpI8c.png!
    The same with a pre existing toolbar:
    @preExistingToolbar->addWidget(w);@ Perfect: !http://i.imgur.com/L2mPp.png!



  • Hello Julien!

    I've tried your code and it works fine. I've done it subclassing a QMainWindow and directly from a main.cpp defining a basic QMainWindow. From both I obtained your desired form. Cannot find where the problem could be. Maybe I didn't understand your problem.

    @#include <QtGui>
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QMainWindow *mainWindow = new QMainWindow();

    QToolBar* uiToolbar = new QToolBar(mainWindow);
     
    QWidget *w = new QWidget;
    QGridLayout *gbox = new QGridLayout;
     
    QComboBox* parametersCB = new QComboBox;
    QLabel* typeLabel = new QLabel("type");
    QLabel* currentLabel = new QLabel("Current parameters:");
     
    gbox->addWidget(currentLabel,0,0,1,2);
    gbox->addWidget(parametersCB,1,0);
    gbox->addWidget(typeLabel,1,1);
     
    w->setLayout(gbox);
    uiToolbar->addWidget(w);
    mainWindow->addToolBar(Qt::TopToolBarArea, uiToolbar);
    
    mainWindow->show();
    
    return app.exec();
    

    }@

    Could you give more details?

    Regards



  • First, thanks a lot for your time!
    Can you post a screenshot of the result?
    That is really weird, something in my code must be interacting with the layout, but I have no idea what it is.



  • Of course. But I don't know how to post a picture here, it asks me for some source. How did you managed to do it? O.o





  • Ok, thanks!

    Here you are !http://i.imgur.com/flPRn.png?1(image1)!



  • Or simply put the combo box and the type label in a new horizontal layout that is added after the first label in a vertical layout. I actually prefer that to setting up a grid layout.



  • There must be something broken in my toolbar. How can I easily debug that?



  • Hello again!

    Select a debug build and introduce break points wherever you want/need. Look anywhere you are accessing the toolbar. If you cannot find the problem just proceed as utcenter said:

    bq. put the combo box and the type label in a new horizontal layout that is added after the first label in a vertical layout

    Sometimes this method simplifies your life.

    Good luck!



  • That solved it! Thanks again.


Log in to reply
 

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