2

My class uses QWidget and I've got some QPushButtons there and I'd like to set a QLabel on top of each button, which are set on the window by move() but QLabel doesn't want to move... I use setMargin but it moves it from left to right, but not up or down. There's an example of my code:

    self.btn = QPushButton(QIcon(),"Show table", self)
    self.btn.move(360, 10)
    self.btn.resize(100, 20)
    self.btn.clicked.connect(self.doAction)

    self.label = QLabel("Here comes the boom")

    layout_LineEdit = QVBoxLayout()
    layout_LineEdit.addWidget(self.label)
    self.setLayout(layout_LineEdit)
Kamil
  • 1,456
  • 4
  • 32
  • 50
  • Why do you need QLabels on top of QPushbuttons? (Perhaps you're not going the right way about achieving what you want). – Anti Earth Jan 20 '13 at 00:51
  • I have those buttons in a few columns and I want to set a label for each column. – Kamil Jan 20 '13 at 10:58
  • 1
    Aha, I probably misinterpreted the phrase 'on top'. Your labels aren't moving, because you've added them to a layout, which now governs their position. If you intend to position them manually, do not add them to the layout (better yet, add your buttons to the vertical layout as well, **after** the labels) – Anti Earth Jan 20 '13 at 11:02

1 Answers1

2

Add a moveEvent to your class and connect move signal to your slot, your slot should be a function that change your widget's geometry via:

YourClass::moveEvent(QMoveEvent *ev)
{ 
    emit move(ev->pos());
    QLabel::moveEvent(ev);
}

your SLOT function:

void move_label(QPoint *point)
{
    setGeomtry(0, 0, point->x, point->y);
}

and connect them as below:

connect(label_widget, SIGNAL(move(QPoint)), this, move_label(QPoint));
Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41