How to mirror a QWidget to two windows at once?
-
Is it possible to draw/mirror a widget into two different windows at same time?
For example, draw the
containerWidget
in both windows, when i check the checkbox in one window it also would
be checked into the second window:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); QWidget *containerWidget = new QWidget(this); QGridLayout *gridLayout = new QGridLayout(containerWidget); QCheckBox* checkbox = new QCheckBox(); checkbox->setGeometry(50, 5, 50, 50); gridLayout->addWidget(checkbox); QMainWindow *mainWindow2 = new QMainWindow; mainWindow2->setCentralWidget(containerWidget); mainWindow2->show(); }
-
Hi
I think what you are asking would imply a widget being displayed twice, which Qt has never been made for.
I think the only solution is to instanciate the widget twice, which is what you already did, and sync them with appropriate signal and slots. Or maybe share the same data structure if this ever seems relevant.
In your MainWindow's construtor, you would add a double connection like this
connect (containerWidget->myCheckBox, &QCheckBox::onChecked, mainWindow2->myCheckBox, &QCheckBox::setChecked) ; connect (mainWindow2->myCheckBox, &QCheckBox::onChecked, containerWidget->myCheckBox, &QCheckBox::setChecked) ;
if you ever need to emit your own signals, don't forget to check whether the value has actually changed before emitting. Otherwise you would get signals being send to each other into an infinite loop.
-
As already stated, but to restate...No. You need to address mirroring on the X display level using someting like a screen scraping VNC server, or do what @ankou29666 mentioned and create two separate instances and link them with signals.