PDA

View Full Version : Simple QMainWindow



giusepped
18th October 2008, 03:08
I am a newbie.
I have a simple code:


mainWindow::mainWindow()

{

QTextEdit *editor1 = new QTextEdit;
QTextEdit *editor2 = new QTextEdit;
QTextEdit *editor3 = new QTextEdit;

QSplitter splitter(Qt::Horizontal,this);
splitter.addWidget(editor1);
splitter.addWidget(editor2);
splitter.addWidget(editor3);

editor1->setPlainText("Mon enfant, ma soeur,\n"
"Songe à la douceur\n"
"D'aller là -bas vivre ensemble,\n"
"Aimer à loisir,\n"
"Aimer et mourir\n"
"Au pays qui te ressemble.");
editor2->setPlainText("My child, my sister,\n"
"think of the sweetness\n"
"of going there to live together!\n"
"To love at leisure,\n"
"to love and to die\n"
"in a country that is the image of you!");
editor3->setPlainText("Mein Kind, meine Schwester,\n"
"denke an den Traum\n"
"dort hin(unter) zu gehen um zusammen\n"
"zu leben und in aller Ruhe zu lieben,\n"
"Zu lieben und zu sterben\n"
"in dem Land, das dir gleicht.");

splitter.setWindowTitle(QObject::tr("Splitter"));
setCentralWidget(splitter);
}
and the main is straightforward:


#include <QtGui>
#include "mainwindow.h"
int main(int argc, char *argv[])
{

QApplication app(argc, argv);


QMainWindow *mainWindow =new QMainWindow;
mainWindow->show();
return app.exec();

}


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?

JimDaniel
18th October 2008, 05:06
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);

giusepped
18th October 2008, 08:09
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?

wysota
18th October 2008, 08:52
http://www.qtcentre.org/forum/faq.php?faq=qt_general_category#faq_qt_stack_creat ed_widgets

lyuts
22nd October 2008, 13:45
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
}

spirit
22nd October 2008, 14:20
offtop
you don't delete widget in this code


...
QMainWindow *mainWindow = new QMainWindow();
mainWindow -> show();
...


create this widget in stack, i.e.


...
QMainWindow mainWindow;
mainWindow.show();
...


or set attribute for this widget Qt::WA_DeleteOnClose (http://doc.trolltech.com/4.4/qt.html#WidgetAttribute-enum)
i.e.


...
QMainWindow* mainWindow = new QMainWindow();
mainWindow->setAttribute(Qt::WA_DeleteOnClose);
mainWindow -> show();
...


or delete this widget manually


...
QMainWindow* mainWindow = new QMainWindow();
mainWindow -> show();
...
bool res = app.exec();
delete mainWindow;
mainWindow = 0;
return res;