Important: Please read the Qt Code of Conduct -

Layouts and Form Size, menu-strip color

  • I am yet another Qt newbie, working my way through a Qt book, but have some questions that are probably obvious to experienced Qt programmers.

    1. I am creating basic forms/dialogs using the layout managers - this of course re-sizes the widgets appropriately if something changes. However, the form/window itself does not adjust to the change. What is the recommended method to get the form to adjust it's size if the layouts on it change?

    2. I would like to change the background color and foreground text color on the menu-strip. I haven't found a way to do this - suggestions?

    Thanks for any guidance.

  • On item #2 (changing colors in menu-strip), how can this be done programmatically? I need to use different colors in different states.

  • Just set up some signals, for example:
    QAction *actionChangeState1 = new QAction("State &1", this);
    QAction *actionChangeState2 = new QAction("State &2", this);

    connect(actionChangeState1, SIGNAL(triggered()), qApp, SLOT(stateX()));
    connect(actionChangeState2, SIGNAL(triggered()), qApp, SLOT(stateY()));

    void myClass::stateX() {
    QString ss("QMenuBar::item { background: green; color: orange }");

    void myClass::stateY() {
    QString ss2("QMenuBar::item { background: red; color: blue }");

  • Sorry, I feel like an idiot, but I am not following you at all. I understand the signals and slots, no problem there, but I don't understand this syntax:

     QMenuBar::item { background: black; color: green };

    I look at the QMenuBar class documentation and I don't see anything that matches up to this.

    I have a menu bar that I reference through "ui->menubar". What property/member/method do I need to use to set the background and foreground color?

    Thanks again.

  • I updated the code posted above (in more detail). This will allow you to change the background and foreground colours in your menus/dropdown menus. To change the colour of the entire toolbar, you can do something like:
    QString ss("QMenuBar::item { background-color: red; color: black }");

    Good luck!

  • OK, this makes sense now. Thanks for your help!

  • I'd like to revisit my first question, about getting the form to resize to match the contents. I'm having trouble understanding the size policy and size hints. I have setup a simple test project to experiment with this. I have a single dialog with no buttons, I set it to a small size (about 180 x 130), and drop a label on it. I set the horizontal size of the label so that it is larger than the window, so the text can go off of the form. In the constructor, I set the text to a long string so that it does go off of the form.

    Is there a way to configure the form so that it recognizes that the objects on the form exceed the boundaries of the form and have the form resize itself appropriately? Likewise if the object becomes smaller can the form resize accordingly?

    Thanks again.

  • I don't think you want the form to keep changing sizes every time a widget changes size unless you want to give the user a seizure. Alternatively, you could use the link I sent to setup a layout where the widgets change size based on not only form size, but also based on other widgets on the form. So if one widget is going to exceed the boundaries, Qt dynamically shrinks the other widget to fit it, for example.

    alternative idea: enable autoscrollbars on the form so if a widget's size goes past the form's border, the form implements scrollbars.

    I don't think there is a convenience class where you can dynamically change the size of your form, but a possible hack could be setting up signals that resize the form based on some maximum sizes you set for your widgets, so if a widget exceeds a certain maxsize you set - you can call a slot that resizes the main form. Have you ever used a software application where the main window is constantly resizing?

  • I wouldn't have the form "constantly resizing". My application will run on both Windows and Linux - switching platforms changes sizes of labels etc. It will also allow for translation to different languages - labels/strings can be very short in one language and long in another. Therefore the form size would only change when used on a different platform or with a different language. I could just use the maximum form size, but this seems awkward in some cases - things like the Qt layouts seem to be designed to handle situations like this, but don't seem to link to the form size.

    Anyway, thanks for the feedback. I'll keep tinkering with it and figure something out.

  • Okay, keep us posted on your progress.

Log in to reply