#ifndef MYWIDGET_H#define MYWIDGET_H#include <QtGui>#include <QWidget>#include "stdio.h"#include "stdlib.h"#define DIALOG 1#define GROUP 2#define BUTTON 3class MyWidget{public: QWidget *wgt; QHBoxLayout *group_layout; Qt::Orientation group_orient; unsigned wgt_type, wgt_f; MyWidget(void *widget); MyWidget(MyWidget *parent, unsigned type); void create(void *parent, unsigned type, int x, int y, int w, int h); ~MyWidget(); void setPosW(int x, int y); void setSizeW(int w, int h); void setStrW(const char *str); void showW(void);};#endif // MYWIDGET_H
#include "mywidget.h"MyWidget::MyWidget(void *widget){ wgt_f = 1; wgt = (QWidget *)widget; if(wgt->metaObject()->className() == QString("QDialog")) wgt_type = DIALOG; else if(wgt->metaObject()->className() == QString("QToolButton")) wgt_type = BUTTON; else if(wgt->metaObject()->className() == QString("QGroupBox")) wgt_type = GROUP;}MyWidget::MyWidget(MyWidget *parent, unsigned wgt_type){ wgt_f = 0; if(parent == NULL) create(NULL, wgt_type, 0, 0, 0, 0); else create(parent->wgt, wgt_type, 0, 0, 0, 0);}void MyWidget::create(void *parent, unsigned type, int x, int y, int w, int h){ wgt_type = type; QPoint pos(x, y); QSize size(w, h); if(wgt_type == DIALOG) wgt = (QWidget *) new QDialog((QWidget *)parent); else if(wgt_type == BUTTON){ wgt = (QWidget *) new QToolButton((QWidget *)parent); if(((QWidget *)parent)->metaObject()->className() == QString("QGroupBox")) group_layout->addWidget(wgt); } else if(wgt_type == GROUP) { wgt = (QWidget *) new QGroupBox((QWidget *)parent); group_layout = new QHBoxLayout; wgt->setLayout(group_layout); }}MyWidget::~MyWidget(){ if(wgt_f == 0) delete wgt; wgt = NULL;}void MyWidget::setPosW(int x, int y){ QPoint pos(x, y); wgt->move(pos);}void MyWidget::setSizeW(int w, int h){ wgt->setFixedSize(w, h);}void MyWidget::setStrW(const char *str){ if(str == NULL) { return; } else{ if(wgt_type == DIALOG) qobject_cast<QDialog *>(wgt)->setWindowTitle(str); else if(wgt_type == BUTTON) qobject_cast<QToolButton *>(wgt)->setText(str); else if(wgt_type == GROUP) qobject_cast<QGroupBox *>(wgt)->setTitle(str); }}void MyWidget::showW(void){ wgt->show();}
#include "mywidget.h"int main(int argc, char *argv[]){ QApplication app(argc, argv); qDebug() << "hello"; MyWidget *base; base = new MyWidget(NULL, DIALOG); base->setPosW(40, 40); base->setSizeW(200, 200); base->setStrW("My class widget"); base->showW(); MyWidget *group; group = new MyWidget(base, GROUP); group->setSizeW(100, 100); group->setPosW(20, 20); group->setStrW("Group:"); group->showW(); MyWidget *button[3]; for(int i=0; i<3; i++){ button[i] = new MyWidget(group, BUTTON); button[i]->setSizeW(20, 20); button[i]->setStrW("Button"); button[i]->showW(); } return app.exec();}