Advertisement
Guest User

Pass values from two or more MainWindows in Qt

a guest
Sep 16th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. // This C++ code will help you connect signals and slots between all instances of a Window
  2.  
  3.  
  4. /// This function is called when MainWindow is created
  5. void MainWindow::MainWindow()
  6. {
  7.     // Identify this QObject as "MainWindow", this helps the program know which widgets are "MainWindow"
  8.     setObjectName("MainWindow");
  9.  
  10.     // This is a personal preference, you can ignore it if you want
  11.     setAttribute(Qt::WA_DeleteOnClose);
  12.  
  13.     // Show the window (you can ignore this if the MainWindow is shown directly in you main.cpp file)
  14.     show();
  15. }
  16.  
  17. /// This function is called when a new window is created
  18. void MainWindow::CreateNewMainWindow(MainWindow *Window)
  19. {
  20.     // Connect the slots that update and apply the settings between this MainWindow and the newly created one (you can change the signals and slots according to your needs)
  21.     connect(Window, SIGNAL(UpdateSettings()), this,   SLOT(ApplySettings()));
  22.     connect(this,   SIGNAL(UpdateSettings()), Window, SLOT(ApplySettings()));
  23.  
  24.     // Connect the same slots on all the remaining instances of MainWindow (you can change the signals and slots according to your needs)
  25.     foreach(QWidget *Widget, QApplication::topLevelWidgets())
  26.     {
  27.         if(Widget->objectName() == "MainWindow")
  28.         {
  29.             connect(Widget, SIGNAL(UpdateSettings()), Window, SLOT(ApplySettings()));
  30.             connect(Window, SIGNAL(UpdateSettings()), Widget, SLOT(ApplySettings()));
  31.         }
  32.     }
  33.  
  34.     // Copy the geometry of the original window into the new window
  35.     Window->setGeometry(x(), y(), width(), height());
  36.    
  37.     // Move the window a little bit (so that both windows are visible)
  38.     Window->move(x() + 50, y() + 50);
  39.  
  40.     // Show the new window
  41.     Window->show();
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement