GoYoshi
GoYoshi

Reputation: 373

Drawing on a label that's a layer behind a table

In my application I have a table in the foreground. It has a transparent background so I can see a label with an image through it. Both the table and the label have the exact same size.

I want to draw on the label while the table is still viewable, but not usable. Since the label is a layer behind the table, I just can't draw on it, even if I disable the table and disable the focus. It only works if the label is in the foreground which however would mean that the table is not visible anymore.

I want to draw as soon as I hit a button:

void MainWindow::on_btn_draw_clicked()
{
  fg_table->setFocusPolicy(Qt::NoFocus);
  fg_table->setEnabled(false);
  bg_label->setFocusPolicy(Qt::StrongFocus);
  bg_label->setFocus();
}

Which however is not working. The table is disabled and not usable (which is correct), but drawing on bg_label is not possible.

In the Designer the bg_label is a placeholder for the class 'DrawLabel' which inherits from QLabel. In the class drawing is made possible and I can react to various mouse events. Everything works fine and in theory I can draw, but just not if the bg_label is a layer behind the table.

I know that you can raise/lower the layer of a widget with

  bg_label->activateWindow();
  bg_label->raise();

but this is not what I want. The layers should not be changed and I just want to draw behind the table on bg_label.

Is there any way to achieve this? I haven't seen a similar problem anywhere.

Upvotes: 0

Views: 209

Answers (1)

G.M.
G.M.

Reputation: 12879

The fact that fg_table is visually transparent is irrelevant. The simple fact is that fg_table will receive all mouse events. Those that it doesn't accept will be propogated to the parent -- not necessarily the widget that appears to be visually underneath -- i.e. bg_label.

Assuming the foreground table doesn't need to interact with mouse events you could probably use...

fg_table->setAttribute(Qt::WA_TransparentForMouseEvents);

That should result in all mouse events going to bg_label.

Upvotes: 1

Related Questions