Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Problems with setCentralWidget

  1. #1
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with setCentralWidget

    Hello all,

    I have this code

    Qt Code:
    1. class QTWindowView : public WindowView, public QTextEdit{
    2. public:
    3. QString getText() const;
    4. void setText(const QString & text);
    5. };
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by jpn; 8th March 2008 at 12:01. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with setCentralWidget

    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?
    J-P Nurmi

  3. #3
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    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

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

    Default Re: Problems with setCentralWidget

    This is not a backtrace. Use the "bt" command in gdb.

  5. #5
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    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
    Last edited by jpn; 8th March 2008 at 13:18. Reason: missing [quote] tags

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

    Default Re: Problems with setCentralWidget

    What is runApp() and QTMainWindowView?

  7. #7
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    runApp:
    Qt Code:
    1. int runApp(QApplication& app, int argc, char** argv) {
    2. MainWindow::setViewFactory(new QTViewFactory);
    3. MainWindow mainWin;
    4. if(mainWin.evaluateArguments(argc, argv)) {
    5. mainWin.show();
    6. return app.exec();
    7. }
    8. return -1;
    9. }
    To copy to clipboard, switch view to plain text mode 
    QTWindowView:
    Qt Code:
    1. class QTMainWindowView : public MainWindowView, public QMainWindow {
    2. public:
    3. void show();
    4. void setCenterWidget(WidgetView* view);
    5. };
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Problems with setCentralWidget

    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.

  9. #9
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    [QUOTE]1. QWidget::show() is not virtual [QUOTE]
    I know

    2. What's the contents of QTMainWindowView::setCenterWidget ?
    Qt Code:
    1. void QTMainWindowView::show() {
    2. QMainWindow::show();
    3. }
    4.  
    5. void QTMainWindowView::setCenterWidget(WidgetView* view) {
    6. QMainWindow::setCentralWidget((QWidget*)view);
    7. }
    To copy to clipboard, switch view to plain text mode 
    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 .... ?

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

    Default Re: Problems with setCentralWidget

    Quote Originally Posted by Rockem View Post
    1. QWidget::show() is not virtual
    I know
    Good, so why do you reimplement it anyway?


    Qt Code:
    1. void QTMainWindowView::show() {
    2. QMainWindow::show();
    3. }
    4.  
    5. void QTMainWindowView::setCenterWidget(WidgetView* view) {
    6. QMainWindow::setCentralWidget((QWidget*)view);
    7. }
    To copy to clipboard, switch view to plain text mode 
    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...

  11. #11
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    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.

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

    Default Re: Problems with setCentralWidget

    Quote Originally Posted by Rockem View Post
    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.

    Qt Code:
    1. class MyIface {
    2. public:
    3. QWidget *create(...) = 0;
    4. };
    5. Q_DECLARE_INTERFACE(...);
    6.  
    7. class IfaceImpl : public QObject, public MyIface {
    8. Q_OBJECT
    9. Q_INTERFACES(...);
    10. public:
    11. QWidget *create(...) { return new QMainWindow(...); }
    12. };
    To copy to clipboard, switch view to plain text mode 

    I also need the cast coz WidgetView is NOT QWidget.
    Then you can't pass it to QMainWindow::setCentralWidget()... Simple as that...

  13. #13
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    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:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class CompView {
    4. public:
    5. virtual ~CompView() {}
    6. virtual void eli() = 0;
    7. };
    8.  
    9. class Comp : public QTextEdit, public CompView {
    10. public:
    11. void eli() {}
    12. };
    13.  
    14. int main(int argc, char *argv[]) {
    15. QApplication app(argc, argv);
    16. CompView* c = new Comp;
    17. mw.setCentralWidget((QWidget*)c);
    18. mw.show();
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

    when I run this I also get segmentation fault
    Last edited by Rockem; 8th March 2008 at 19:22.

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

    Default Re: Problems with setCentralWidget

    What is the point of hiding Qt architecture if you are immediately passing the pressumably non-Qt component to a Qt-expecting method?

    Qt Code:
    1. Comp* c = new Comp;
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by wysota; 8th March 2008 at 19:38.

  15. #15
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    Quote Originally Posted by wysota View Post
    What is the point of hiding Qt architecture if you are immediately passing the pressumably non-Qt component to a Qt-expecting method?

    Qt Code:
    1. Comp* c = new Comp;
    To copy to clipboard, switch view to plain text mode 
    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 ?

  16. #16
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    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.

  17. #17
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    Quote Originally Posted by marcel View Post
    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 ..)

  18. #18
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    I can't(not yet) because I still don't see the point... Are you writing a library? Or what?

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

    Default Re: Problems with setCentralWidget

    Quote Originally Posted by Rockem View Post
    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):

    Qt Code:
    1. class Iface {
    2. public:
    3. virtual void initialize() = 0;
    4. virtual void show() = 0;
    5. virtual void destroy() = 0;
    6. };
    7.  
    8. class Implementation : public Iface {
    9. public:
    10. void initialize() {
    11. if(!QApplication::instance()){
    12. widget = new QMainWindow;
    13. }
    14. }
    15. void show() { widget->show(); }
    16. void destroy() { delete widget; }
    17. private:
    18. QMainWindow *widget;
    19. };
    To copy to clipboard, switch view to plain text mode 

  20. #20
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with setCentralWidget

    Plain old casting and doubble inheritance do not work as expected. Look at this line:

    Qt Code:
    1. mw.setCentralWidget((QWidget*)c);
    To copy to clipboard, switch view to plain text mode 

    Try using qobject_cast or dynamic_cast:
    Qt Code:
    1. mw.setCentralWidget( qobject_cast<QWidget*>(c) );
    2. mw.setCentralWidget( dynamic_cast<QWidget*>(c) );
    To copy to clipboard, switch view to plain text mode 

    or just no cast at all:
    Qt Code:
    1. mw.setCentralWidget( c );
    To copy to clipboard, switch view to plain text mode 

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

  21. The following user says thank you to elcuco for this useful post:

    Rockem (8th March 2008)

Similar Threads

  1. Replies: 2
    Last Post: 8th March 2007, 22:22
  2. Utf8 problems
    By cristiano in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2006, 00:14
  3. Problems building mysql plugin for Qt 4.1.2 on windows XP
    By Philip_Anselmo in forum Installation and Deployment
    Replies: 3
    Last Post: 17th May 2006, 15:38
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39
  5. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28

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.