Results 1 to 6 of 6

Thread: Transition from QMainWindow to QWidget

  1. #1
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Transition from QMainWindow to QWidget

    I have a strange problem. I've built an application in a QMainWindow class. But now I need to rebuild it into something that will share the main window with others. So I've built a new QMainWindow class that will house everything. window.h file:
    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "widget.h"
    6.  
    7. namespace Ui {
    8. class window;
    9. }
    10.  
    11. class window : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit window(QWidget *parent = 0);
    17. ~window();
    18.  
    19. private:
    20. Ui::window *ui;
    21. };
    22.  
    23. #endif // WINDOW_H
    To copy to clipboard, switch view to plain text mode 
    And then I've tried to rewrite the old main windows as a QWidget and have the window show it. widget.h file:
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class widget;
    8. }
    9.  
    10. class widget : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit widget(QWidget *parent = 0);
    16. ~widget();
    17.  
    18. private:
    19. Ui::widget *ui;
    20. };
    21.  
    22. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 
    This the code I use minus the internal methods. And the compilation error is on first line of the widget constructor:
    Qt Code:
    1. widget::widget(QWidget *parent) : QWidget(parent), ui(new Ui::widget)
    To copy to clipboard, switch view to plain text mode 
    Which says "invalid use of incomplete type 'struct Ui::widget'".

    Now, I've tried looking it up and I've found that this kind of error usually occurs through recursive definitions and integrations where by the time the compiler is doing the line in error, it needs definitions that won't become available until later on or even inside the class.
    Following the instructions on building an example, I did manage to write pretty much the same thing from scratch with, as far as I can see, the same code as the one I pasted and it works perfectly.

    So, my question is, did anyone already try doing what I did and can tell me what I'm doing wrong or any suggestions on how I can do it without writing the whole thing from scratch? Because right now it seems like I have to and I'd like to avoid it.

    Proper notice: I am a newbie to Qt and am still using a lot of automatics like the Designer, the automatic slot connection and the likes...
    Last edited by Sabre Runner; 12th October 2010 at 16:06.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Embedding a Widget in a MainWindow

    Does your widget have a .ui file too?
    Then, at least, you need to include ui_widget.h (or whatever it is called) in your widget cpp file.

    By the way, it is also a good idea if you're learning Qt and don't have any experience with C++ to have a good C++ beginners book around.

  3. #3
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Embedding a Widget in a MainWindow

    the ui header file is also included in the code file.
    I am a kind of novice at C++. I've written terminal applications for various uses a couple of years now. This is my first foray into a GUI and actual use application.
    But yes, that is good advice. I'll bring my book with me next time.

    Other than that, you have no idea why this might happen?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Embedding a Widget in a MainWindow

    Quote Originally Posted by Sabre Runner View Post
    Other than that, you have no idea why this might happen?
    Yes, I do know why you get that error.
    The error means it can't find the definition of Ui::widget. This is defined in a file called ui_widget.h. It gets automatically generated when you build your program. You don't have to worry about the compiler not finding it on time, this is all taken care of by qmake.

    You either didn't include the correct file, it didn't get generated or you didn't include that file at all.
    With the information you have given already, it is not possible to tell.

    Can you post the following files please?
    your .pro file
    and the .cpp file of the widget.

  5. The following user says thank you to tbscope for this useful post:

    Sabre Runner (12th October 2010)

  6. #5
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Embedding a Widget in a MainWindow

    Actually, now that you mention it. I think I know where the problem is. It's probably the modifications I forgot to make inside the UI file when I changed it.

    Be right back.

  7. #6
    Join Date
    Sep 2010
    Location
    Israel
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Embedding a Widget in a MainWindow

    Yes, I forgot the UI file in the transition from a QMainWindow class to a QWidget class.

Similar Threads

  1. QMenubar in a non mainwindow widget
    By Tito in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th August 2010, 15:24
  2. Replies: 10
    Last Post: 29th May 2010, 18:42
  3. graphicsView and widget embedding
    By wagmare in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2009, 16:14
  4. Replies: 0
    Last Post: 4th February 2009, 14:46
  5. QwtPlot widget in MainWindow
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 11:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.