qpushbutton looks like a label

  • 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?

  • Lifetime Qt Champion


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

  • 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?

  • Moderators

    @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.

  • @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->setContentsMargins(0, 0, 0, 0);
    //setContentsMargins(0, 0, 0, 0);
    const auto rightLayout= new QVBoxLayout;
    rightLayout->addWidget(new QLabel("text"));

    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)

  • Moderators

    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);

  • Moderators

    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);

Log in to reply

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