Results 1 to 6 of 6

Thread: Simple QMainWindow

  1. #1
    Join Date
    May 2008
    Posts
    276
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Simple QMainWindow

    I am a newbie.
    I have a simple code:

    Qt Code:
    1. mainWindow::mainWindow()
    2.  
    3. {
    4.  
    5. QTextEdit *editor1 = new QTextEdit;
    6. QTextEdit *editor2 = new QTextEdit;
    7. QTextEdit *editor3 = new QTextEdit;
    8.  
    9. QSplitter splitter(Qt::Horizontal,this);
    10. splitter.addWidget(editor1);
    11. splitter.addWidget(editor2);
    12. splitter.addWidget(editor3);
    13.  
    14. editor1->setPlainText("Mon enfant, ma soeur,\n"
    15. "Songe à la douceur\n"
    16. "D'aller là -bas vivre ensemble,\n"
    17. "Aimer à loisir,\n"
    18. "Aimer et mourir\n"
    19. "Au pays qui te ressemble.");
    20. editor2->setPlainText("My child, my sister,\n"
    21. "think of the sweetness\n"
    22. "of going there to live together!\n"
    23. "To love at leisure,\n"
    24. "to love and to die\n"
    25. "in a country that is the image of you!");
    26. editor3->setPlainText("Mein Kind, meine Schwester,\n"
    27. "denke an den Traum\n"
    28. "dort hin(unter) zu gehen um zusammen\n"
    29. "zu leben und in aller Ruhe zu lieben,\n"
    30. "Zu lieben und zu sterben\n"
    31. "in dem Land, das dir gleicht.");
    32.  
    33. splitter.setWindowTitle(QObject::tr("Splitter"));
    34. setCentralWidget(splitter);
    35. }
    To copy to clipboard, switch view to plain text mode 
    and the main is straightforward:

    Qt Code:
    1. #include <QtGui>
    2. #include "mainwindow.h"
    3. int main(int argc, char *argv[])
    4. {
    5.  
    6. QApplication app(argc, argv);
    7.  
    8.  
    9. QMainWindow *mainWindow =new QMainWindow;
    10. mainWindow->show();
    11. return app.exec();
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 


    But when I compile, i get the error
    mainwindow.cpp:61: error: no matching function for call to 'mainWindow::setCentralWidget(QSplitter&)'
    /usr/include/QtGui/qmainwindow.h:122: note: candidates are: void QMainWindow::setCentralWidget(QWidget*)


    I don't understand why. Maybe because I am using qt3 and not qt4?
    Last edited by wysota; 18th October 2008 at 09:51. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    14
    Thanked 41 Times in 35 Posts

    Default Re: Simple QMainWindow

    Your problem is this - setCentralWidget() wants a pointer to a QWidget. If you change your code to this it will compile:

    setCentralWidget(&splitter);

    However, since you are declaring the QSplitter on the stack, once the constructor reaches the end, the splitter will be destroyed. Instead you should create it on the heap.

    QSplitter * splitter = new QSplitter(Qt::Horizontal, this);
    setCentralWidget(splitter);
    Last edited by JimDaniel; 18th October 2008 at 06:19.

  3. #3
    Join Date
    May 2008
    Posts
    276
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: Simple QMainWindow

    yes you're right. I just did it.
    But now the problem is that when I run the application, nothing appears in the mainwindow....
    Any suggestion?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

  5. #5
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 29 Times in 27 Posts

    Default Re: Simple QMainWindow

    Quote Originally Posted by giusepped View Post
    yes you're right. I just did it.
    But now the problem is that when I run the application, nothing appears in the mainwindow....
    Any suggestion?
    you are not calling QMainWindow constructor. As i understand your mainWindow class is inherited from QMainWindow. If so, then your constructor should look like this

    mainWindow::mainWindow(QWidget *parent)
    :QMainWindow(parent)
    {
    //constructor body
    }
    I'm a rebel in the S.D.G.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: Simple QMainWindow

    offtop
    you don't delete widget in this code
    Qt Code:
    1. ...
    2. QMainWindow *mainWindow = new QMainWindow();
    3. mainWindow -> show();
    4. ...
    To copy to clipboard, switch view to plain text mode 

    create this widget in stack, i.e.
    Qt Code:
    1. ...
    2. QMainWindow mainWindow;
    3. mainWindow.show();
    4. ...
    To copy to clipboard, switch view to plain text mode 

    or set attribute for this widget Qt::WA_DeleteOnClose
    i.e.
    Qt Code:
    1. ...
    2. QMainWindow* mainWindow = new QMainWindow();
    3. mainWindow->setAttribute(Qt::WA_DeleteOnClose);
    4. mainWindow -> show();
    5. ...
    To copy to clipboard, switch view to plain text mode 

    or delete this widget manually
    Qt Code:
    1. ...
    2. QMainWindow* mainWindow = new QMainWindow();
    3. mainWindow -> show();
    4. ...
    5. bool res = app.exec();
    6. delete mainWindow;
    7. mainWindow = 0;
    8. return res;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QMainWindow modal from non-qt window?
    By hickscorp in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 10:10
  2. QMainWindow setCentralWidget from ui widget, Qt4
    By alan in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2008, 14:00
  3. QMainWindow child of a QDialog
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2008, 08:16
  4. Dynamically Loading a QMainWindow?
    By natron in forum Newbie
    Replies: 10
    Last Post: 21st July 2006, 02:15
  5. Replies: 18
    Last Post: 22nd February 2006, 21:51

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.