PDA

View Full Version : QWidget over current widget - it's in place, but not painted



googie
18th June 2013, 02:16
Hi,

I just created simple "QMainWindow" application, just from QtCreator New Project wizard. I've added two push buttons into center widget, then each of them connected (clicked() signal) with slots:


void MainWindow::on_pushButton_clicked()
{
w = new QWidget(this);
w->move(0, 0);
w->setFixedSize(50, 200);
w->setVisible(true);
}

void MainWindow::on_pushButton_2_clicked()
{
delete w;
}

What bothers me is that after pressing button1, widget is created and resized as expected, but it's not painted at all. I mean I see that the widget is there, because it covers left part of the upper push button, therefore the pushbutton doesn't light-up on mouse over event, but when I move mouse right the pushbutton lights up. But... the widget itself is not painted at all, like it was transparent :(

What am I missing here? Shouldn't the widget paint itself as empty rectangle, instead of transparent one?

Santosh Reddy
18th June 2013, 10:40
What am I missing here? Shouldn't the widget paint itself as empty rectangle, instead of transparent one?
A QWidget by default has nothing to paint, it just empty. This is expected behavior.

googie
18th June 2013, 13:51
I see. What about QFrame? I tried with that too, with same result.

Santosh Reddy
18th June 2013, 14:11
void MainWindow::on_pushButton_clicked()
{
w = new QFrame(this); //<<<<<<<<<<<<< change w to QFrame *
w->move(0, 0);
w->setFixedSize(50, 200);
w->setVisible(true);
w->setFrameStyle(QFrame::Box); //<<<<<<<<<<<<<< this is required
}

void MainWindow::on_pushButton_2_clicked()
{
delete w;
}

googie
19th June 2013, 12:08
Found it! QWidget::setAutoFillBackground(true) was the key.