Results 1 to 5 of 5

Thread: type QWidget is not a direct base of MyWidget

  1. #1
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    7

    Default type QWidget is not a direct base of MyWidget

    Hi there.
    I'm folloiwng the Qt Tutorials from the Qt website (if anyone knows of better tutorials, please tell me the link).

    I wrote this code on QtCreator and it gave me an error

    Qt Code:
    1. class MyWidget : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MyWidget(QWidget *parent = 0);
    7. ~MyWidget();
    8.  
    9. private:
    10. Ui::MyWidget *ui;
    11. };
    12.  
    13. MyWidget::MyWidget(QWidget *parent)
    14. : QWidget(parent)//ERROR
    15. {
    16. QPushButton *quit = new QPushButton("Close Application", this);
    17. quit->setGeometry(150,150,100,100);
    18. quit->setFont(QFont("Times",12,QFont::Bold));
    19.  
    20. connect(quit,SIGNAL(clicked()),qApp,SLOT(quit()));
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    Error: Type "QWidget" is not direct base of 'MyWidget'.

    Could someone tell me where I've gone wrong - thanks!

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: type QWidget is not a direct base of MyWidget

    You derived your MyWidget class from QMainWindow class, in this code:
    Qt Code:
    1. class MyWidget : public QMainWindow //QMainWindow is the base class of MyWidget
    2. {
    3. //...
    To copy to clipboard, switch view to plain text mode 
    So you will need to pass the parent to the QMainWindow constructor:
    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent)
    2. : QMainWindow(parent)//No ERROR now
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Zlatomir for this useful post:

    eLancaster (4th December 2010)

  4. #3
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    7

    Default Re: type QWidget is not a direct base of MyWidget

    Believe me when I say this - you just made my day!!!
    THANK YOU!!!!!!!!


    Added after 4 minutes:


    Oh but there is one more thing, now that the code actually runs - when I click on the close application button - it doesn't work. Is there something wrong with the signals and slots?
    and just one last quesiton - we write the constructor definition in the mywidget.cpp file, right?
    Last edited by eLancaster; 4th December 2010 at 15:12.

  5. #4
    Join Date
    Dec 2010
    Location
    Omaha, NE
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 2 Times in 2 Posts

    Default Re: type QWidget is not a direct base of MyWidget

    I would do the following:

    connect(quit, SIGNAL(clicked()), this, SLOT(close()));

    This will close the main window, which will in turn exit the main application, as your widget is probably blocking the main event loop (which is probably why calling the quit() slot is not currently working).

    I would make it a habit of placing debug assertions on your connect statements as well:

    bool ok = connect(...);
    Q_ASSERT(ok);

    This will save you trouble when you mistype something to connect, and alert you immediately of the mistake.

    As far as your constructor going in the .cpp file - I always place implementation code (such as constructor definitions) in a .cpp file, as it allows more flexibility in your code. Unless the code is doing something really, really simple, like only returning a primitive data type or you are doing something like writing a template.

  6. The following user says thank you to johnc for this useful post:

    eLancaster (4th December 2010)

  7. #5
    Join Date
    Dec 2010
    Location
    My bed
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    7

    Default Re: type QWidget is not a direct base of MyWidget

    Oh yeah - using this, that shoulda been obvious!

    Thanks alot about the Q_assert tip - This is my complete first deep doing this GUI stuff so I really appreciate it!

Similar Threads

  1. Qt + WINAPI + direct painting
    By JovianGhost in forum Qt Programming
    Replies: 8
    Last Post: 26th March 2010, 05:10
  2. focusChanged() getting qwidget type
    By noobasaurus in forum Qt Programming
    Replies: 5
    Last Post: 6th April 2009, 23:49
  3. Undefined reference MyWidget::....()
    By DrDonut in forum Newbie
    Replies: 1
    Last Post: 7th March 2008, 16:44
  4. QWidget and type
    By nowire75 in forum Newbie
    Replies: 1
    Last Post: 8th November 2007, 19:55
  5. Replies: 2
    Last Post: 30th June 2006, 18:42

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
  •  
Qt is a trademark of The Qt Company.