Results 1 to 5 of 5

Thread: QWidget in a subwindow

  1. #1
    Join Date
    Jul 2011
    Location
    Germany
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QWidget in a subwindow

    Hello,

    I have a subwindow (m_window) in which I want to add a created (Painter)-Widget (GraphWidget):

    Qt Code:
    1. m_window = new QWidget();
    2.  
    3. m_graphwidget = new GraphWidget ();
    4. m_graphwidget->setGeometry(QRect(500, 100, 50, 50));
    5. m_graphwidget->show();
    6.  
    7. // show some Labels
    8. showWindow();
    9.  
    10. m_window->show();
    To copy to clipboard, switch view to plain text mode 

    With this the GraphWidget is placed in another window, that's clear because i didn't connect the widget with m_window. To place the GraphWidget in the window I used m_graphwidget = new GraphWidget (m_window). But then I got the compiler error:

    error: no matching function for call to ‘GraphWidget::GraphWidget(QWidget*&)’
    I don't understand this message because m_window is only a pointer to a QWidget?!

    I tried another way: I declared m_window as a QMainWindow object and connected it with the GraphWidget:

    Qt Code:
    1. m_window->setCentralWidget(m_graphwidget);
    To copy to clipboard, switch view to plain text mode 

    In this case, the GraphWidget is placed in the window, but I can't set its position and I can see only one part of the labels (which are defined in showWindow(); ).

    Can you tell me a solution of this problem?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QWidget in a subwindow

    there may be a problem with GraphWidget class defintion, post the GraphWidget class definition / headr file. You may be missing a corresponding ctor definition

    by the way, are you sure you want to fix the position usig setGeometry(), can't you use layout to do the job for you.

  3. #3
    Join Date
    Jul 2011
    Location
    Germany
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget in a subwindow

    Hi, thank you for your answer.

    Here is the definition of GraphWidget:

    Qt Code:
    1. class GraphWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. void paintEvent(QPaintEvent *);
    8.  
    9. };
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. void GraphWidget::paintEvent(QPaintEvent* )
    2. {
    3. QPainter painter(this);
    4. painter.setPen(Qt::blue);
    5. painter.setFont(QFont("Arial", 30));
    6. painter.drawText(rect(), Qt::AlignCenter, "Qt");
    7. }
    To copy to clipboard, switch view to plain text mode 

    I thought setGeometry() is easier but if layout works I can use that too.
    Last edited by blackhole; 26th July 2011 at 22:32.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QWidget in a subwindow

    Your error message comes about because you have provided no constructor that expects a QWidget* argument (as Santosh Reddy said). The only constructors GraphWidget has are an automatic default constructor and copy constructor. The lines after the error message will often tell you what options you have, for example GCC emits:
    Qt Code:
    1. main.cpp:22: error: no matching function for call to ‘GraphWidget::GraphWidget(QWidget*&)’
    2. main.cpp:5: note: candidates are: GraphWidget::GraphWidget()
    3. main.cpp:5: note: GraphWidget::GraphWidget(const GraphWidget&)
    To copy to clipboard, switch view to plain text mode 
    Add a minimal constructor:
    Qt Code:
    1. explicit GraphWidget(QWidget *p = 0): QWidget(p) {}
    To copy to clipboard, switch view to plain text mode 
    and the problem will go away.

    Layouts are easier especially since you want more than one widget inside your window. If you continue to try to absolutely position items then I predict your next question will be, "My window doesn't resize its content properly, how do I fix it?".

  5. The following user says thank you to ChrisW67 for this useful post:

    blackhole (27th July 2011)

  6. #5
    Join Date
    Jul 2011
    Location
    Germany
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget in a subwindow

    Thanks a lot! It works. I promise I will try layouts before I post this question

Similar Threads

  1. Replies: 5
    Last Post: 1st March 2011, 14:39
  2. Connect to widget in SubWindow
    By ursbibin in forum Newbie
    Replies: 1
    Last Post: 6th July 2010, 21:28
  3. Open subwindow
    By olidem in forum Qt Programming
    Replies: 8
    Last Post: 14th May 2009, 13:12
  4. cannot open a subwindow!
    By cbarmpar in forum Qt Programming
    Replies: 1
    Last Post: 22nd September 2008, 16:06
  5. subwindow mdi signal
    By walito in forum Newbie
    Replies: 8
    Last Post: 5th August 2007, 23:15

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.