How to open dialog box on top of the application if two screen are connected?



  • I wanted to open Slash screen before opening our Software and One Dialog box after complete opening of the software.

    I have second screen is connected with Laptop. My Visual studio is opened into second screen, whenever I run the code at that time software is open into second screen but the dialog box and SplashScreen is open into the First screen.

    how can I set something like both should open wherever the Software is open.
    if the software is open into the First screen then I wanted to both dialog and slash screen both should open into the First screen. if the software open into second screen then both should open into second screen.

    Here I attach small portion of the code which i wrote for dialog.

    void MainWindowUI::ShowDataRecoveryInformDialog(QWidget parent) {
    QDialog
    dialog = new QDialog(parent, Qt::SplashScreen);
    auto dialogLay = new QVBoxLayout(dialog);
    dialogLay->addWidget(PBT("button 1"));
    dialogLay->addWidget(PBT("button 2"));
    dialog->exec();
    }


  • Lifetime Qt Champion

    Hi
    Is parent set ?
    Normally it opens where ever parent is.

    what do you give as paramter for ShowDataRecoveryInformDialog?



  • Yes I set the parent as m_mainWindow.
    /**********************MainWindow/
    class MainWindow : public QMainWindow {
    Public :
    void CreateUI();
    private:
    MainWindowUI *ui;
    };

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
    ui(new MainWindowUI(this))
    {
    ui->CreateUI();
    }

    /MainWindowUI*/

    class MainWindowUI : public QObject {
    MainWindow *m_mainWidnow;
    };
    void MainWindowUI::CreateUI() {
    CreateCentralWidget();
    }

    void MainWindowUI::CreateCentralWidget(){
    ShowDataRecoveryInformDialog (m_mainWidnow);
    }


  • Lifetime Qt Champion

    hi
    and m_mainWidnow has a value at that point in time ?
    To be sure please try

    void MainWindowUI::CreateCentralWidget(){
    qDebug() << "parent = " << m_mainWidnow;
    ShowDataRecoveryInformDialog (m_mainWidnow);
    }

    Im using 2 monitors at work and it always open on top of parent with no extra effort on my part.



  • yes m_mainWindow has value.

    I try another thing into ShowDataRecoveryInformDialog Funcation.
    Instead of the passing parent as argument. I set the parent with help of setParent property it is work.

    Thank you for Help @mrrj



  • in addition, I try like
    QDialog* dialog = OBJ_NAME(new QDialog(m_mainWindow, Qt::SplashScreen), "notes-dialog");
    It is also create the same issue.

    only setParent is work fine.


  • Lifetime Qt Champion

    Hi,

    Why not use this rather than m_mainWindow ?


  • Lifetime Qt Champion

    @Yash001
    and you have like
    ShowDataRecoveryInformDialog::ShowDataRecoveryInformDialog(QWidget *parent) : QDialog (parent)

    in your dialog so you forward parent to base?



  • @SGaist I already try it but it is not work. i got this error.

    Severity Code Description Project File Line Suppression State
    Error C2664 'void QWidget::setParent(QWidget *,Qt::WindowFlags)': cannot convert argument 1 from 'MainWindowUI *const ' to 'QWidget *'


  • Lifetime Qt Champion

    Can you show your complete code for your MainWindow ?

    It looks like you are modifying the wrong file.



  • @mrjj
    m_mainWindow is my main Object, it is create the whole application on GUI side by calling createUI funcation.

    yes i was try to set the parent as main object.

    I make this change and code work perfectly fine. and got the output, which I want.

    void MainWindowUI::ShowDataRecoveryInformDialog() {
    QDialog dialog = new QDialog( );

    dialog->setWindowFlags(Qt::SplashScreen);
    dialog->setParent(m_mainWindow);

    auto dialogLay = new QVBoxLayout(dialog);
    dialogLay->addWidget(PBT("button 1"));
    dialogLay->addWidget(PBT("button 2"));
    dialog->exec();
    }



  • @SGaist by using setParent and set WindowFlag property. i am able to open dialog on top of the Application.

    dialog->setWindowFlags(Qt::SplashScreen);
    dialog->setParent(m_mainWindow);

    Thank you Sir for your help.


  • Lifetime Qt Champion

    Ok should be the same as, or else im very wondering

    void MainWindowUI::ShowDataRecoveryInformDialog() {
    QDialog dialog = new QDialog( m_mainWindow);
    dialog->setWindowFlags(Qt::SplashScreen);
    auto dialogLay = new QVBoxLayout(dialog);
    dialogLay->addWidget(PBT("button 1"));
    dialogLay->addWidget(PBT("button 2"));
    dialog->exec();
    }
    


  • @mrjj

    I don't know. what is different but now it is work. i did not make any change except setting the property

    before:
    QDialog *dialog = new QDialog(parent, Qt::SplashScreen);

    now :

    QDialog* dialog = new QDialog();
    dialog->setWindowFlags(Qt::SplashScreen);
    dialog->setParent(m_mainWindow);


  • Lifetime Qt Champion

    @Yash001
    ok, im not sure either. seems the same :)



  • I found the different.

    it is work fine.

    dialog->setWindowFlags(Qt::SplashScreen);
    dialog->setParent(m_mainWindow);

    it is not work.

    dialog->setParent(m_mainWindow);
    dialog->setWindowFlags(Qt::SplashScreen);


  • Lifetime Qt Champion

    @Yash001
    Ok so Qt::SplashScreen does something with parent.



  • @mrjj May be. It is qt default function.


  • Lifetime Qt Champion

    @Yash001
    yeah its a windows flag.



  • @mrjj

    dialog->setWindowFlags(Qt::SplashScreen);
    dialog->setParent(m_mainWindow);
    In this case I got dialog box on top of the screen but it is make another issue

    dialog->exec(); is not work as per expectation. without responding the to Dialog user access the other page also.

    In other word, user can access both screen Main application as well as Dialog box. how to block access on other widget until get response from the user in dialog box?


  • Lifetime Qt Champion

    Hi
    Im not 100% sure Qt::SplashScreen is compatiple with Qt::Dialog flag

    Qt::SplashScreen 0x0000000e | Window Indicates that the window is a splash screen. This is the default type for QSplashScreen.

    Normally used with QSplashScreen widget.


  • Lifetime Qt Champion

    maybe try with
    dialog->setWindowFlags(Qt::Dialog | Qt::SplashScreen);



  • @mrjj it is create previous issue.


  • Lifetime Qt Champion

    @Yash001
    but why you need Qt::SplashScreen ?
    and modal dialog ?
    cant you just use the dialog ?



  • @Yash001
    Similarly to @mrjj , have you considered using QSplashScreen (http://doc.qt.io/qt-5/qsplashscreen.html), maybe it doesn't have these problems?



  • I am using Qt::SplashScreen because of I wanted show dialog on the top of all screen. This dialog is show whenever specific signal is generated from Hardware Connection. if hardware is connected with PC before opening our Software then dialog will open after Software Open. and control need to transfer on dialog box. (at this time user should not access any other page).

    I have not idea what is model Dialog.


  • Lifetime Qt Champion

    @Yash001
    Modal dialog means user cannot click on mainwindow behind dialog.
    Sometimes also called blocking.
    comes when using exec() versus show().



  • @mrjj understood. thank you.