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. qpushbutton looks like a label

qpushbutton looks like a label

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.6k Views
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    in q QWidget subclass ctor i'm creating a QPushButton, create a layout and add the button to the layout, and setting the layout as the widget's layout. everything works fine, however, the button looks like a label rather than a button. what's the problem?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please post the code you are using. From your description it could be anything from a custom style to a style sheet error.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        oh that's right. i had set

        setStyleSheet("border: 1px solid blue");
        

        on my QWidget subclass.

        but i didn't even set my QPushButton's parent to be this QWidget subclass, why does the stylesheet apply to it too? but well, i've added the button to a layout and set that layout to my widget. is that the reason?

        Chris KawaC 1 Reply Last reply
        0
        • U user4592357

          oh that's right. i had set

          setStyleSheet("border: 1px solid blue");
          

          on my QWidget subclass.

          but i didn't even set my QPushButton's parent to be this QWidget subclass, why does the stylesheet apply to it too? but well, i've added the button to a layout and set that layout to my widget. is that the reason?

          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @user4592357 said in qpushbutton looks like a label:

          but well, i've added the button to a layout and set that layout to my widget. is that the reason?

          Yes. Widget with a layout becomes parent of everything that is put in that layout.

          U 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            @user4592357 said in qpushbutton looks like a label:

            but well, i've added the button to a layout and set that layout to my widget. is that the reason?

            Yes. Widget with a layout becomes parent of everything that is put in that layout.

            U Offline
            U Offline
            user4592357
            wrote on last edited by user4592357
            #5

            @Chris-Kawa

            okay i got it. but then there's this: i have this code and with which i wanna have 4 vertically arranged buttons, however, the space between two buttons is too much. however, if i add more buttons to (in their corresponding layouts), the space between two buttons decreases. i understand that the space is distributed among the child widgets, but how can i possibly do what i need? here's the code:

            auto button = new QPushButton(QIcon(":/start"), sStart);
            const auto startButtonLayout = new QHBoxLayout;
            //startButtonLayoutsetContentsMargins(0, 0, 0, 0);
            startButtonLayout->addWidget(button, 0, Qt::AlignHCenter);
            
            button = new QPushButton(QIcon(":/chart"), sHighScores);
            const auto highScoresLayout = new QHBoxLayout;
            //highScoresLayout->setContentsMargins(0, 0, 0, 0);
            highScoresLayout->addWidget(button, 0, Qt::AlignHCenter);
            
            button = new QPushButton(QIcon(":/instructions"), sInstructions);
            const auto instructionsLayout = new QHBoxLayout;
            //instructionsLayout->setContentsMargins(0, 0, 0, 0);
            instructionsLayout->addWidget(button, 0, Qt::AlignHCenter);
            
            button = new QPushButton(QIcon(":/exit"), sExit);
            const auto exitLayout = new QHBoxLayout;
            //instructionsLayout->setContentsMargins(0, 0, 0, 0);
            exitLayout->addWidget(button, 0, Qt::AlignHCenter);
            
            buttonsLayout->addLayout(startButtonLayout);
            buttonsLayout->addLayout(highScoresLayout);
            buttonsLayout->addLayout(instructionsLayout);
            buttonsLayout->addLayout(exitLayout);
            
            //buttonsLayout->setContentsMargins(0, 0, 0, 0);
            //setContentsMargins(0, 0, 0, 0);
            
            const auto rightLayout= new QVBoxLayout;
            rightLayout->addWidget(new QLabel("text"));
            
            pageLayout->addLayout(buttonsLayout);
            pageLayout->addLayout(rightLayout);
            
            setLayout(pageLayout);
            

            and this is how they look:
            alt text

            the reason that i'm adding each button to a layout is so that i can align them horizontally in the center (i don't know if this is the best way)

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You don't need the extra layouts for alignment. You can just add the widgets directly to the buttonsLayout and set the alignment in the same call.
              If you want the buttons to be tightly packed at the top there are couple of ways to do that. The easiest way here is to add a stretch after the last button:

              buttonsLayout->addWidget(new QPushButton(QIcon(":/start"), sStart), 0, Qt::AlignHCenter);
              buttonsLayout->addWidget(new QPushButton(QIcon(":/chart"), sHighScores), 0, Qt::AlignHCenter);
              buttonsLayout->addWidget(new QPushButton(QIcon(":/instructions"), sInstructions), 0, Qt::AlignHCenter);
              buttonsLayout->addWidget(new QPushButton(QIcon(":/exit"), sExit), 0, Qt::AlignHCenter);
              buttonsLayout->addStretch(0);
              
              1 Reply Last reply
              2
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                I might have misunderstood what you want to do. This entire thing can be probably done with a single grid layout:

                auto pageLayout = new QGridLayout();
                pageLayout->addWidget(new QPushButton(QIcon(":/start"), sStart), 0, 0);
                pageLayout->addWidget(new QPushButton(QIcon(":/chart"), sHighScores), 1, 0);
                pageLayout->addWidget(new QPushButton(QIcon(":/instructions"), sInstructions), 2, 0);
                pageLayout->addWidget(new QPushButton(QIcon(":/exit"), sExit), 3, 0);
                pageLayout->addWidget(new QLabel("text"), 0, 1, 5, 1);
                pageLayout->setColumnStretch(1,1);
                setLayout(pageLayout);
                
                1 Reply Last reply
                2

                • Login

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