0

I want to create custom Layout alike here: http://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-flowlayout-cpp.html

I want some methood to put checkbox on top of custom button. For now there are

setGeometry(QRect(QPoint(...

methods for either button and checkbox but whether I'm doing it for button or checkobox first still checkbox appears "under" the button and I can't see/click it.

How can I put checkbox on top of button there?

cnd
  • 32,616
  • 62
  • 183
  • 313
  • Can you provide some code of the Button? – Alexander Tyapkov Dec 09 '15 at 08:29
  • @AlexanderTyapkov it's just QWidget or QLayoutItem for layout but originally I put it as QToolButton and then layout->addWidget(myButton); – cnd Dec 09 '15 at 08:36
  • ok. so basically you are using custom layout, but at the same time you want to put the checkbox on the top of the button with setGeometry? Will it be not easier to put checkbox inside layout? – Alexander Tyapkov Dec 09 '15 at 08:40
  • @AlexanderTyapkov checkbox is inside layout too! and I position it in the same place with setGeometry where is my button but it appears below it (I can't see it on top of button) – cnd Dec 09 '15 at 08:50

2 Answers2

1

I have just made this snippet to check the checkbox on the top of the button and it works for me.

#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
#include <QCheckBox>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    QWidget w;

    QPushButton button("Hello World!", &w);
    button.setGeometry(0,0,100,100);
    button.show();

    QCheckBox checkBox(&w);
    checkBox.setGeometry(30,30,50,50);
    checkBox.show();

    w.show();
    return a.exec();
}

and here is if you will change the order of "parenting" and want checkbox still be on the top:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget w;

QCheckBox checkBox(&w);
checkBox.setGeometry(30,30,50,50);
checkBox.show();

QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();

checkBox.setParent(NULL);
checkBox.setParent(&w);

w.show();
return a.exec();

}

Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65
1

Simply make the checkbox a child of the button and call setGeometry relative to the button. Children are always drawn on top of their parents.

QPushButton button("Hello World!", &w);
button.setGeometry(0,0,100,100);
button.show();

QCheckBox checkBox(&button);
checkBox.setGeometry(button.rect());
checkBox.show();

No need to put the checkbox into a layout.

Aaron
  • 1,181
  • 7
  • 15