Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Layouts and Form Size, menu-strip color
Forum Updated to NodeBB v4.3 + New Features

Layouts and Form Size, menu-strip color

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 5.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Klondike6
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Blizzard
      wrote on last edited by
      #2

      http://developer.qt.nokia.com/doc/qt-4.8/QWidget.html#size-hints-and-size-policies
      http://developer.qt.nokia.com/doc/qt-4.8/qsizepolicy.html

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

      Software Engineer | Aerospace & Defence

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Klondike6
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Blizzard
          wrote on last edited by
          #4

          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 }");
          menuBar()->setStyleSheet(ss);
          }

          void myClass::stateY() {
          QString ss2("QMenuBar::item { background: red; color: blue }");
          menuBar()->setStyleSheet(ss);
          }
          @

          Software Engineer | Aerospace & Defence

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Klondike6
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Blizzard
              wrote on last edited by
              #6

              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 }");
              menuBar()->setStyleSheet(ss);
              @

              Good luck!

              Software Engineer | Aerospace & Defence

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Klondike6
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Klondike6
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Blizzard
                    wrote on last edited by
                    #9

                    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?

                    Software Engineer | Aerospace & Defence

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Klondike6
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        Blizzard
                        wrote on last edited by
                        #11

                        Okay, keep us posted on your progress.

                        Software Engineer | Aerospace & Defence

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved