Adding widget into custom QWidget
-
Hello everybody,
i'm newbbie and I have some problem with my Qt Custom Widget.
I wrote a custom widget, and integrate it to Designer.With Designer, some Widget (like QGroupBox) accepts drop off other widget on it.
But with my custom I can't, I don't really know why. To create a custom widget my class herits of QWidget. I think it could be a problem.I tryed to add a layout to my custom widget but there is no action...
Can you give me a help please ?
Thank you.
.h
@
#ifndef QTBORDERWIDGET_H
#define QTBORDERWIDGET_H#include <QtGui>
#include <QDesignerExportWidget>
#include <QWidget>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>#include <QMetaType>
class QDESIGNER_WIDGET_EXPORT QtBorderWidget : public QWidget
{
Q_OBJECTQ_PROPERTY(QString m_title READ getTitle WRITE setTitle NOTIFY titleChanged DESIGNABLE true)
public:
QtBorderWidget(QWidget *parent = 0);QString getTitle() const; void setTitle(QString t);
signals:
void titleChanged(const QString);private slots:
void onTitleChanged(const QString);private:
QWidget *m_widget;
QHBoxLayout *m_hLayout;
QGroupBox *m_groupbox;
QString m_title;protected:
void paintEvent(QPaintEvent *e);
};#endif
@.cpp
@
#include "QtBorderWidget.h"QtBorderWidget::QtBorderWidget(QWidget *parent)
: QWidget(parent)
{
m_widget = new QWidget(parent);
m_hLayout = new QHBoxLayout(m_widget);
m_groupbox = new QGroupBox(m_widget);
m_title = "GroupBox";m_hLayout->setContentsMargins(0, 0, 0, 0); QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); sizePolicy.setHorizontalStretch(0); sizePolicy.setVerticalStretch(0); sizePolicy.setHeightForWidth(m_groupbox->sizePolicy().hasHeightForWidth()); m_groupbox->setSizePolicy(sizePolicy); m_groupbox->setFlat(true); m_groupbox->setMinimumSize(120,80); m_hLayout->addWidget(m_groupbox); setLayout(m_hLayout); connect(this, SIGNAL(titleChanged(QString)), this, SLOT(onTitleChanged(QString)));
}
void QtBorderWidget::paintEvent(QPaintEvent *e)
{
QPainter p(this);
QRect r1(0, 10, this->width()-1, this->height()-11);
QRect r2(0, 0, this->width()-1, 20);p.setPen(QColor(0, 0, 0)); p.setBackgroundMode(Qt::OpaqueMode); p.drawRoundedRect(r1, 10, 10, Qt::AbsoluteSize); p.setOpacity(1.0); p.setBackgroundMode(Qt::OpaqueMode); p.drawText(r2, Qt::AlignCenter, m_title); m_groupbox->resize(this->width()-1, this->height()-1);
}
QString QtBorderWidget::getTitle() const
{
return m_title;
}void QtBorderWidget::setTitle(QString t)
{
m_title = t;
emit titleChanged(m_title);
}void QtBorderWidget::onTitleChanged(QString t)
{
m_title = t;
repaint();
}
@ -
What is m_widget supposed to be? From the looks of it, the class itself is what you intended for m_widget yet you create a QWidget and parent it to the parent of your class
@
m_widget = new QWidget(parent);
@Then you create layouts the are set to m_widget with this:
@
m_hLayout = new QHBoxLayout(m_widget);
@And finally you try to then use those layouts in your main class widget:
@
setLayout(m_hLayout);
@That line above won't work as you can't reparent the widget to this when you already set it as a layout for m_widget when you created the layout.
I'm thinking everywhere you are using m_widget you are actually meaning to use this->xxx().
I don't use designer much so I can't help you on integrating with that though. I prefer to just write the code myself rather than use Designer.