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. QComboBox width would changed when it setstylesheet
Forum Updated to NodeBB v4.3 + New Features

QComboBox width would changed when it setstylesheet

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 770 Views 2 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.
  • H Offline
    H Offline
    Hanson
    wrote on last edited by Hanson
    #1

    When i do not setstylesheet to the widget the qcombobox looks ok.like this.
    Snipaste_2024-01-22_17-27-29.png
    This is the designer picture.
    Snipaste_2024-01-22_17-25-56.png
    The code:

    QtGuiApplication::QtGuiApplication(QWidget *parent)
    	: QWidget(parent)
    {
    	ui.setupUi(this);
    
        ui.comboBox->addItem("xxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
        ui.comboBox->setCurrentIndex(-1);
    }
    

    But if I setstylesheet, it will change.

    QtGuiApplication::QtGuiApplication(QWidget *parent)
    	: QWidget(parent)
    {
    	ui.setupUi(this);
    
        ui.comboBox->addItem("xxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
        ui.comboBox->setCurrentIndex(-1);
        setStyleSheet("QPushButton {background-color:black;}");
    }
    

    Snipaste_2024-01-22_17-27-51.png Snipaste_2024-01-22_17-28-01.png
    The qcombobox width change to fit the item text width.
    The Qt version I used is 5.9.7.
    Any help is apprecitate!

    “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
    —— Martin Golding

    JoeCFDJ 1 Reply Last reply
    0
    • C Offline
      C Offline
      CPPUIX
      wrote on last edited by
      #4

      Hi,

      Try QComboBox::AdjustToMinimumContentsLengthWithIcon, because the default value is AdjustToContentsOnFirstShow, so the combo box stretching that way, is expected.

      You can do that using code, or through Qt Designer:
      QComboBoxAdjustSizePolicy.png

      H 1 Reply Last reply
      0
      • H Hanson

        When i do not setstylesheet to the widget the qcombobox looks ok.like this.
        Snipaste_2024-01-22_17-27-29.png
        This is the designer picture.
        Snipaste_2024-01-22_17-25-56.png
        The code:

        QtGuiApplication::QtGuiApplication(QWidget *parent)
        	: QWidget(parent)
        {
        	ui.setupUi(this);
        
            ui.comboBox->addItem("xxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
            ui.comboBox->setCurrentIndex(-1);
        }
        

        But if I setstylesheet, it will change.

        QtGuiApplication::QtGuiApplication(QWidget *parent)
        	: QWidget(parent)
        {
        	ui.setupUi(this);
        
            ui.comboBox->addItem("xxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
            ui.comboBox->setCurrentIndex(-1);
            setStyleSheet("QPushButton {background-color:black;}");
        }
        

        Snipaste_2024-01-22_17-27-51.png Snipaste_2024-01-22_17-28-01.png
        The qcombobox width change to fit the item text width.
        The Qt version I used is 5.9.7.
        Any help is apprecitate!

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #2

        @Hanson It may trigger adjustSize() call. Try to set fixed size to ui.comboBox if its size is known. And 5.9.7 is too old.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote on last edited by
          #3

          QComboBox has a SizeAdjustPolicy. But, I haven't found any value for this policy I would expect to help.

          In this very specific case you might want to set the style sheet only on the push button: ui.pushButton->setStyleSheet("background-color:black;");. It doesn't help, though, if you plan on adjusting more things globally with a stylesheet.

          H 1 Reply Last reply
          0
          • C Offline
            C Offline
            CPPUIX
            wrote on last edited by
            #4

            Hi,

            Try QComboBox::AdjustToMinimumContentsLengthWithIcon, because the default value is AdjustToContentsOnFirstShow, so the combo box stretching that way, is expected.

            You can do that using code, or through Qt Designer:
            QComboBoxAdjustSizePolicy.png

            H 1 Reply Last reply
            0
            • S SimonSchroeder

              QComboBox has a SizeAdjustPolicy. But, I haven't found any value for this policy I would expect to help.

              In this very specific case you might want to set the style sheet only on the push button: ui.pushButton->setStyleSheet("background-color:black;");. It doesn't help, though, if you plan on adjusting more things globally with a stylesheet.

              H Offline
              H Offline
              Hanson
              wrote on last edited by
              #5

              @SimonSchroeder Hi, this example is used to simulate my actual usage scenario.

              “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
              —— Martin Golding

              1 Reply Last reply
              0
              • C CPPUIX

                Hi,

                Try QComboBox::AdjustToMinimumContentsLengthWithIcon, because the default value is AdjustToContentsOnFirstShow, so the combo box stretching that way, is expected.

                You can do that using code, or through Qt Designer:
                QComboBoxAdjustSizePolicy.png

                H Offline
                H Offline
                Hanson
                wrote on last edited by
                #6

                @Abderrahmene_Rayene Oh!!!!It works!!!Thank you so much!!!

                “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                —— Martin Golding

                1 Reply Last reply
                0
                • H Hanson has marked this topic as solved on

                • Login

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