What's the right way to 'Show' the application?



  • My application uses an indicator and it sits there when it is minimized. The first option when you right-click on the indicator is to show the application's the main window. The problem is that I don't know what is the right code to run so as to set my application to be on top of all the other applications. This is what I've tried so far:

    @
    this->setFocusPolicy(Qt::StrongFocus);
    this->setFocus();
    this->showNormal();
    this->activateWindow();
    showNormal();
    activateWindow();
    this->raise();
    this->stackUnder(0);
    @
    but this just shows the application, but not on top of the current active window, but below it.

    I've done a million (or more) google searches in order to find an effective solution to this, and, honestly, more or less, everybody recommends part of the above code. I assumed that combining all the recommendations would work, but still, it fails miserably.

    Using Ubuntu Linux 12.04.1

    Thanks in advance for any replies!!!



  • QWidget::raise only "Raises this widget to the top of the parent widget's stack" - see QWidget's documentation.
    IIRC raising the application window - i.e. instructing the window manager to do so - is not a functionality provided by Qt. But the following hack does what you want. Just set and immediately unset the Qt::WindowStaysOnTopHint flag. This way it will be put on top of all other windows. This works fine under KWin, the only glitch there is is that (for some reason) the window is hidden when setting that flag. This can be overcome by calling show() right after the flag-hackery but still the window will disapper shortly.

    @
    #include <QApplication>
    #include <QLabel>
    #include <QTimer>
    #include <QVBoxLayout>
    #include <QWidget>

    class Widget:public QWidget{
    Q_OBJECT
    public:
    Widget(QWidget* parent=0)
    :QWidget(parent)
    {
    setFixedSize(300,300);
    QVBoxLayout* layout=new QVBoxLayout(this);
    layout->addWidget(new QLabel("<h1>RamaLamaDingDong</h1>"));
    }

    public slots:
    void putOnTop(){
    Qt::WindowFlags flags=windowFlags();
    setWindowFlags(flags|Qt::WindowStaysOnTopHint);
    setWindowFlags(flags);
    //for some reason on my machine the window is hidden after changing
    //the flags, so explicitly show it again:
    show();
    }
    };

    int main(int argc,char* argv[])
    {
    QApplication a(argc,argv);
    QTimer timer;
    Widget w;
    w.show();
    QObject::connect(&timer,SIGNAL(timeout()),&w,SLOT(putOnTop()));
    timer.start(2000);
    return a.exec();
    }
    #include "test.moc"

    @



  • Thanks a lot :)

    A weird thing is that in my case (ubuntu 12.04), if I don't call show() afterwards the application force quits :P



  • Are you sure it quits or is it just hidden? It does look like it quits on my machine but if I run it from within QtCreator I can see that the app is still running (red "stop" button still active) tough it is not visible anymore...



  • [quote author="cptG" date="1346769282"]Are you sure it quits or is it just hidden? It does look like it quits on my machine but if I run it from within QtCreator I can see that the app is still running (red "stop" button still active) tough it is not visible anymore...[/quote]

    Yes, you are right, it just stands there hidden.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.