PDA

View Full Version : Problems with setCentralWidget



Rockem
8th March 2008, 11:15
Hello all,

I have this code


class QTWindowView : public WindowView, public QTextEdit{
public:
QString getText() const;
void setText(const QString & text);
};


and when I try to setCentralWidget with this class on QMainWindow
I get a segmentation fault.
whats more, on Windows the code DOES work, but on linux
it crash

any suggestions ?
Eli

jpn
8th March 2008, 12:03
Hard to say with given code, but at least QTextEdit should be the first in the inheritance list if you ever plan to add any Qt meta system stuff to your class. Could you paste backtrace from gdb by the time of crash?

Rockem
8th March 2008, 12:15
this is the backtrace:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1225295584 (LWP 4130)]
0xb77e5b11 in QWidget::isAncestorOf (this=0x8097140, child=0x804ca08) at kernel/qwidget.cpp:6049
/home/elis/downloads/qt-x11-opensource-src-4.3.4/src/gui/kernel/qwidget.cpp:6049:181840:beg:0xb77e5b11


Eli

wysota
8th March 2008, 12:27
This is not a backtrace. Use the "bt" command in gdb.

Rockem
8th March 2008, 12:38
ok, thanx ..


#0 0xb77e5b11 in QWidget::isAncestorOf (this=0x8097140, child=0x804ca08) at kernel/qwidget.cpp:6049
#1 0xb77f3b02 in QWidget::setParent (this=0x8097140, parent=0x80ce47c, f=@0xbf83af20) at kernel/qwidget.cpp:7643
#2 0xb77f3f38 in QWidget::setParent (this=0x804ca08, parent=0x8097140) at kernel/qwidget.cpp:7616
#3 0xb77cfb91 in QLayout::addChildWidget (this=0x80e85a8, w=0x8097140) at kernel/qlayout.cpp:931
#4 0xb7af92f9 in QMainWindowLayout::setCentralWidget (this=0x80e85a8, widget=0x8097140) at widgets/qmainwindowlayout.cpp:1789
#5 0xb7af7080 in QMainWindow::setCentralWidget (this=0x80ce47c, widget=0x8097140) at widgets/qmainwindow.cpp:554
#6 0x0804e01b in QTMainWindowView::setCenterWidget (this=0x80ce478, view=0x8097140) at src/qt/qtmainwindowview.cpp:10
#7 0x0804d01c in MainWindow (this=0xbf83b0b0) at src/mainwindow.cpp:17
#8 0x0804e23c in runApp (app=@0xbf83b100, argc=1, argv=0xbf83b1b4) at tests/tests.cpp:34
#9 0x0804e783 in main (argc=1, argv=0xbf83b1b4) at tests/tests.cpp:49

wysota
8th March 2008, 13:17
What is runApp() and QTMainWindowView?

Rockem
8th March 2008, 14:59
runApp:


int runApp(QApplication& app, int argc, char** argv) {
MainWindow::setViewFactory(new QTViewFactory);
MainWindow mainWin;
if(mainWin.evaluateArguments(argc, argv)) {
mainWin.show();
return app.exec();
}
return -1;
}

QTWindowView:


class QTMainWindowView : public MainWindowView, public QMainWindow {
public:
void show();
void setCenterWidget(WidgetView* view);
};

wysota
8th March 2008, 15:47
1. QWidget::show() is not virtual,
2. What's the contents of QTMainWindowView::setCenterWidget ?
3. What is MainWindowView?
4. I think you are lacking the Q_OBJECT macro somewhere, isAncestorOf() may be needing it, but it's hard to say that without seeing the whole code.

Rockem
8th March 2008, 16:33
1. QWidget::show() is not virtual [QUOTE]
I know :)

[QUOTE]2. What's the contents of QTMainWindowView::setCenterWidget ?


void QTMainWindowView::show() {
QMainWindow::show();
}

void QTMainWindowView::setCenterWidget(WidgetView* view) {
QMainWindow::setCentralWidget((QWidget*)view);
}

WidgetView is my parent view for all the view classes (it's empty)


3. What is MainWindowView?
it is pure abstract class that defines the show and setCentralWidget


4. I think you are lacking the Q_OBJECT macro somewhere, isAncestorOf() may be needing it, but it's hard to say that without seeing the whole code.
ummmm .... ?

wysota
8th March 2008, 16:48
1. QWidget::show() is not virtual
I know :)
Good, so why do you reimplement it anyway?





void QTMainWindowView::show() {
QMainWindow::show();
}

void QTMainWindowView::setCenterWidget(WidgetView* view) {
QMainWindow::setCentralWidget((QWidget*)view);
}

WidgetView is my parent view for all the view classes (it's empty)
Why do you cast to QWidget* in line 6? If your compiler complains without that cast then that's probably your problem - your widget is not a widget...


ummmm .... ?
Yeah... "ummmm" is right... Start by changing the inheritance order as already suggested, remove the cast to QWidget and tell us why you need the additional two layers over QWidget...

Rockem
8th March 2008, 17:01
I'm using a Abstract Factory (Pattern)
which means that I'm accessing all my views (=qt widgets) through
interfaces (=abstract classes)
that is why I need the to reimplement show()

this is also why I have two more layers

I also need the cast coz WidgetView is NOT QWidget.

wysota
8th March 2008, 18:26
I'm using a Abstract Factory (Pattern)
which means that I'm accessing all my views (=qt widgets) through
interfaces (=abstract classes)
that is why I need the to reimplement show()

this is also why I have two more layers
Are you sure your factory can't work with QWidgets? This approach is used all over Qt's plugins.


class MyIface {
public:
QWidget *create(...) = 0;
};
Q_DECLARE_INTERFACE(...);

class IfaceImpl : public QObject, public MyIface {
Q_OBJECT
Q_INTERFACES(...);
public:
QWidget *create(...) { return new QMainWindow(...); }
};


I also need the cast coz WidgetView is NOT QWidget.

Then you can't pass it to QMainWindow::setCentralWidget()... Simple as that...

Rockem
8th March 2008, 18:32
If you include QT in the Factory you loose the whole point
the point is to separate QT GUI from the program's logic
WidgetView is not a QWidget, but QTTextEdit which inherited from it IS
so I can.
this is strange, as the same code is working perfectly under Windows

I wrote a simplified program that show the problem:


#include <QtGui>

class CompView {
public:
virtual ~CompView() {}
virtual void eli() = 0;
};

class Comp : public QTextEdit, public CompView {
public:
void eli() {}
};

int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QMainWindow mw;
CompView* c = new Comp;
mw.setCentralWidget((QWidget*)c);
mw.show();
return app.exec();
}


when I run this I also get segmentation fault

wysota
8th March 2008, 19:33
What is the point of hiding Qt architecture if you are immediately passing the pressumably non-Qt component to a Qt-expecting method?


Comp* c = new Comp;
The above works fine. Using CompView won't work because the object is aligned differently, so the compiler generates wrong offset for calling QWidget's methods.

Rockem
8th March 2008, 19:54
What is the point of hiding Qt architecture if you are immediately passing the pressumably non-Qt component to a Qt-expecting method?


Comp* c = new Comp;
The above works fine. Using CompView won't work because the object is aligned differently, so the compiler generates wrong offset for calling QWidget's methods.

first, this is just an example, using the whole factory does seperate QT classes
from the rest of the application.

does someone know of a way around it ?
can't I separate logic and gui when using Qt ?

marcel
8th March 2008, 20:01
I didn't read all the posts, just the last question.



can't I separate logic and gui when using Qt ?
Of course you can... Qt does it with the MVC architecture, for example. There are other ways too.

I'm not sure what CompView is supposed to do, but it doesn't look good. Having an object inheriting from a GUI class and a class that implements app logic is not called "separating logic".
Actually, there's a word for that: "mess". In the end you'll end up with an object that exposes both things anyway.

Rockem
8th March 2008, 20:10
I didn't read all the posts, just the last question.

Of course you can... Qt does it with the MVC architecture, for example. There are other ways too.

I'm not sure what CompView is supposed to do, but it doesn't look good. Having an object inheriting from a GUI class and a class that implements app logic is not called "separating logic".
Actually, there's a word for that: "mess". In the end you'll end up with an object that exposes both things anyway.

can you give me an example ? for the short program I have ?
how can I have a widget but hide all Qt related ?

I don't know what you call a mess, but in java this is what we do and
it separates perfectly so we can work with multiple views (swing, swt, web ..)

marcel
8th March 2008, 20:35
I can't(not yet) because I still don't see the point... Are you writing a library? Or what?

wysota
8th March 2008, 20:51
can you give me an example ? for the short program I have ?
how can I have a widget but hide all Qt related ?
Currently you are not hiding anything. You still need a QApplication object, you'll need to run QApplication::exec() eventually, etc. This is certainly not the way to go, regardless of the language used.

If you want to hide Qt, hide all of it and not a part of it - have a function(s) that will do everything starting from making sure there is a QApplication object available, through instantiating the object, showing it, manipulating it up to destroying it when it's not needed anymore. Currently you're just handling instantiation and what about the rest?

There are for instance Photoshop plugins written in Qt and they handle the situation very well, so obscuring the technology behind the interface is possible, you are just doing it the wrong way.

Here is a trivial example (not tested - and you still need QApplication::exec() somewhere):


class Iface {
public:
virtual void initialize() = 0;
virtual void show() = 0;
virtual void destroy() = 0;
};

class Implementation : public Iface {
public:
void initialize() {
if(!QApplication::instance()){
QApplication *app = new QApplication;
widget = new QMainWindow;
}
}
void show() { widget->show(); }
void destroy() { delete widget; }
private:
QMainWindow *widget;
};

elcuco
8th March 2008, 21:15
Plain old casting and doubble inheritance do not work as expected. Look at this line:



mw.setCentralWidget((QWidget*)c);


Try using qobject_cast or dynamic_cast:


mw.setCentralWidget( qobject_cast<QWidget*>(c) );
mw.setCentralWidget( dynamic_cast<QWidget*>(c) );


or just no cast at all:


mw.setCentralWidget( c );


And do try to put the QObject first in the double inheritance.

wysota
8th March 2008, 21:53
qobject_cast won't work because CompView is not a QObject. dynamic_cast seems to work.

Rockem
8th March 2008, 23:57
yea, dynamic_cast works great !!!

thanx :)