-1

I have a button in my Custom QDialog, I am emitting a signal when pushbutton 1 is clicked

void MyCustomDialog::on_pushButton_1()
{
    this->hide(); //i need to hide this window before 'OnButton_1_Clicked' stuffs starts
    emit button_1_clicked();
}

In my main window I have connected the slot and created the instance as shown below

void MainWindow::MainWindow()
{

    MyCustomDialog *dlg = MyCustomDialog::getInstance(this); //only single instance created
    connect(dlg, &MyCustomDialog::button_1_clicked, this, &MainWindow::OnButton_1_Clicked);
}

I am displaying my custom dialog from a function in mainwindow as below

void MainWindow::dispayCustomDialog()
{
    MyCustomDialog *dlg = MyCustomDialog::getInstance();
    dlg->show();
}

Below shows how my 'OnButton_1_Clicked' slot. In which I am capturing the screenshot using below line

void MainWindow::OnButton_1_Clicked()
{
     //capture the screen shot
     QScreen *screen = QGuiApplication::primaryScreen();
     QPixmap *map = new QPixmap(screen->grabWindow(0));
     bool result = map->save("D:/test.jpg", "JPG");
}

Once I captured screen using above function, I can still see my 'MyCustomDialog' in test.jpg file. Qt doc says QGuiApplication::primaryScreen captures the initial state of application. So i think, this is expected in my case. Do we have any other solution to grab screen with current state ?

What I am trying to achieve is grab the screen in OnButton_1_Clicked() function after hiding my 'MyCustomDialog'.

GUI-Novice
  • 351
  • 5
  • 17
  • How do you know it is executed before closing?, I just tried it and it works correctly. To do this, verify if it was hidden: `void MainWindow::OnButton_1_Clicked() { qDebug()<< MyCustomDialog::getInstance()->isVisible(); qDebug()<<"clicked"; }` and I get: `false clicked`. the connection using Qt::QueuedConnection is unnecessary, that flag must be activated if the connection is between 2 objects that are in 2 different threads. – eyllanesc Apr 04 '18 at 04:30
  • Oh. my bad. You are right. I should have been verified with visible function. Unfortunately, I verified with grabbing the screen in 'OnButton_1_Clicked()'. So the mistake was in my grabing screen function. `QScreen *screen = QGuiApplication::primaryScreen(); QPixmap *map = new QPixmap(screen->grabWindow(0)); bool result = map->save("D:/screen.jpg", "JPG");`. – GUI-Novice Apr 04 '18 at 05:48
  • As I understand the `QGuiApplication::primaryScreen();` captures the initial state of the screen in which my custom dialog was visible initially. – GUI-Novice Apr 04 '18 at 05:49
  • So, I should have to find another way to capture current screen instead of intial screen by `QGuiApplication::primaryScreen()`. – GUI-Novice Apr 04 '18 at 05:51
  • You have a [XY problem](http://xyproblem.info/), Your question should focus on your main objective, and not on solving a possible solution that is not certain to work. – eyllanesc Apr 04 '18 at 05:51
  • Edit your question and add all the detail – eyllanesc Apr 04 '18 at 05:58
  • what is your OS? – eyllanesc Apr 04 '18 at 06:04
  • @eyllanesc, sorry for the XY problem and your time. I have edited the question. Please see the same – GUI-Novice Apr 04 '18 at 06:12
  • @eyllanesc, i am developing for both windows and linux. Currently developing with Win10 – GUI-Novice Apr 04 '18 at 06:13
  • For Linux is a problem, check this question. https://stackoverflow.com/questions/39984092/how-to-take-screenshot-of-obscured-window-in-c-on-linux – eyllanesc Apr 04 '18 at 06:20
  • what about windows ? – GUI-Novice Apr 04 '18 at 07:09
  • Try the following example and check if it works: http://doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html – eyllanesc Apr 04 '18 at 07:11
  • @eyllanesc, I found a solution from the your provided example doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html. Used a singleshot timer before grabing the screen. It waits to close my custom dialog properly – GUI-Novice Apr 04 '18 at 07:40
  • @eyllanesc, Thank you for your example – GUI-Novice Apr 04 '18 at 07:40

1 Answers1

0

I found a solution. Used a singleslot timer with delay of 500 msec before capturing the screen as below. This wait for my custom dialog to hide properly.

void MainWindow::OnButton_1_Clicked()
{
     QTimer::singleShot(500, this, &MainWindow::shootscreen);
}

void MainWindow::shootscreen()
{
     //capture the screen shot
     QScreen *screen = QGuiApplication::primaryScreen();
     QPixmap map = screen->grabWindow(0);
     bool result = map.save("D:/test.jpg", "JPG");
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
GUI-Novice
  • 351
  • 5
  • 17
  • Do not use a pointer, you are creating dynamic memory that you do not eliminate, that is bad practice. Change it to: `QPixmap map =screen->grabWindow(0); bool result = map.save("D:/test.jpg", "JPG");` – eyllanesc Apr 04 '18 at 07:42