No taskbar button with qt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dzenanz
    New Member
    • Feb 2008
    • 45

    No taskbar button with qt

    I had a small project based on Qt4, ITK and OpenSceneGraph. It's main function is shown below.
    Code:
    int main( int argc, char **argv )
    {
    	QApplication app(argc, argv);
    	MainWindow mainForm = new MainWindow();
    	mainForm.show();
    	return app.exec();    
    }
    There was a problem, however. The window had no taskbar button. I spent quite some time figuring it out, and in the end it all came down to a very stupid difference: main form was instantiated with new operator.

    The code that produces a normal window with taskbar:
    Code:
    int main( int argc, char **argv )
    {
    	QApplication app(argc, argv);
    	MainWindow mainForm;
    	mainForm.show();
    	return app.exec();    
    }
    I am not sure weather this trick would work in other circumstances/platforms (I am using Windows XP/VisualStudio200 8).

    I hope this will help someone with same/similar problem.

    Keywords (to increase visibility in search engines):
    Qt, osg, hide taskbar button, display taskbar button, opengl, main window
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The C++ new operator returns rthe address of the MainWindow object and not the object itself.

    You need code like trhat works with a MainWindow pointer:
    Code:
    int main( int argc, char **argv ) 
    { 
        QApplication app(argc, argv); 
        MainWindow* mainForm = new MainWindow; 
        mainForm->show(); 
        return app.exec();     
    }

    Comment

    • dzenanz
      New Member
      • Feb 2008
      • 45

      #3
      It worked

      But the point is, everything worked (main window would show, you could alt-tab to it, etc), there was only no taskbar button.

      And you are right, I can try that when I get to work. I did not notice it because compiler did not issue a warning/error about it!

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You will need to look at the code for MainWindow. You are calling the default constructor. There should be code there to create your task bar.

        Code:
        MainWindow* mainForm = new MainWindow;  
         MainWindow mainForm;
        In each case above the MainWindow default constructor is called. In the first case the new operator returns the address of the object and in the second case the object is created as a local stack variable. That means in both cases you should get the same results.

        The only difference is that using the new operator requires you to delete the pointer when you are done with it.

        Comment

        • dzenanz
          New Member
          • Feb 2008
          • 45

          #5
          Incredible!

          Incredible! You were right. When I use MainWindow *mainForm=new ... instead of MainWindow mainForm=new ... it also works normally. Makes me wonder even more why did compiler allow it, and why the whole thing worked (except the taskbar button, of course). MainWindow is a wrapper for something made in designer.
          Code:
          class MainWindow : public QMainWindow, public Ui::mainWindow
          {
          	Q_OBJECT
          
          public:
          	using Ui::mainWindow::centralWidget;
          	MainWindow(QWidget *parent = 0);
          
          protected:
          	void closeEvent(QCloseEvent *event);
          	void resizeEvent( QResizeEvent * event );
          
          signals:
          	void fileOpened(std::string filename);
          
          private slots:
          	void on_actionOpen_activated();
          };
          Last edited by dzenanz; Mar 27 '09, 08:00 AM. Reason: language correction

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Your code does not compile using C++. That is:

            Code:
               MainWindow mainForm = new MainWindow;
            is in error as far as C++ is concerned. It has to be:

            Code:
            MainWindow* mainForm = new MainWindow;
            Now I am not familiar with exactly what compiler you are using but whatever it is, it is not following C++ rules. Maybe you are using a C++-look-alike language that has different rules.

            Comment

            • dzenanz
              New Member
              • Feb 2008
              • 45

              #7
              :D

              I am using Microsoft Visual C++ 2008 IDE/compiler. Compiler not screaming about it must have something to do with Qt's additions.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                I am also using Microsoft Visual C++ 2008 IDE/compiler.

                I have a C++ console application project and your code does not compile when you use the new operator assigning to an object rather than a pointer to an object.

                Here is my error:
                error C2440: 'initializing' : cannot convert from 'MainWindow *' to 'MainWindow'
                generated from:

                Code:
                MainWindow mainForm = new MainWindow;
                whereas

                Code:
                MainWindow* mainForm = new MainWindow;
                compiles fine.

                Comment

                • dzenanz
                  New Member
                  • Feb 2008
                  • 45

                  #9
                  This program also uses OpenSceneGraph and InsightToolKit. If you have them on your system, maybe I can send you the entire project. Or I could rip those 2 out to make a minimal proof-of-concept example. I am in the beginning of starting a new project, so that should not be so hard. Maybe I can go tomorrow to workplace (University to be exact :D) to do it.

                  Of course, you have to be interested :D

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    A proof-of-concept example would be interesting but more than that, step through your code using the debugger. Especially when using the C++ new operator assigning to a MainWindow object (the case that should not compile but does somehow on your machine).

                    That's all I would do myself.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      I don't know what this thread did in the 'insights' section but it belongs here in the normal forum: it is not an article by far ...

                      kind regards,

                      Jos

                      Comment

                      Working...