PDA

View Full Version : QWidget in a subwindow



blackhole
26th July 2011, 18:56
Hello,

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



m_window = new QWidget();

m_graphwidget = new GraphWidget ();
m_graphwidget->setGeometry(QRect(500, 100, 50, 50));
m_graphwidget->show();

// show some Labels
showWindow();

m_window->show();


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:



m_window->setCentralWidget(m_graphwidget);


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?

Santosh Reddy
26th July 2011, 20:13
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.

blackhole
26th July 2011, 20:30
Hi, thank you for your answer.

Here is the definition of GraphWidget:



class GraphWidget : public QWidget
{
Q_OBJECT

public:

void paintEvent(QPaintEvent *);

};


and



void GraphWidget::paintEvent(QPaintEvent* )
{
QPainter painter(this);
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 30));
painter.drawText(rect(), Qt::AlignCenter, "Qt");
}


I thought setGeometry() is easier but if layout works I can use that too.

ChrisW67
27th July 2011, 04:56
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:


main.cpp:22: error: no matching function for call to ‘GraphWidget::GraphWidget(QWidget*&)’
main.cpp:5: note: candidates are: GraphWidget::GraphWidget()
main.cpp:5: note: GraphWidget::GraphWidget(const GraphWidget&)

Add a minimal constructor:


explicit GraphWidget(QWidget *p = 0): QWidget(p) {}

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?".

blackhole
27th July 2011, 10:44
Thanks a lot! It works. I promise I will try layouts before I post this question ;)