自定义Widget的问题
//头文件:class CChannel : public QWidget{ Q_OBJECTpublic: explicit CChannel(QWidget *parent = 0); ~CChannel();public: QLabel *idLabel; QLabel *nameLabel; QLabel *value1Label; QLabel *value2Label;signals:public slots:};//类的实现:CChannel::CChannel(QWidget *parent) : QWidget(parent){ idLabel = new QLabel(this); nameLabel = new QLabel(this); value1Label = new QLabel(this); value2Label = new QLabel(this); idLabel->setText("1"); nameLabel->setText("U1"); value1Label->setText("100.00"); curHight = 20; idLabel->setFixedWidth(16); nameLabel->setFixedWidth(100); value1Label->setFixedWidth(32); value2Label->setFixedWidth(32); QHBoxLayout *pLayout = new QHBoxLayout; pLayout->addWidget(idLabel); pLayout->addWidget(nameLabel); pLayout->addWidget(value1Label); pLayout->addWidget(value2Label); value2Label->setVisible(false); nameLabel->setBackgroundRole(QPalette::Light); nameLabel->setAutoFillBackground(true); pLayout->setSizeConstraint(QLayout::SetFixedSize); setLayout(pLayout); this->setFixedWidth(138);}CChannel::~CChannel(){}
//头文件:namespace Ui { class Dialog;}class Dialog : public QDialog{ Q_OBJECTpublic: explicit Dialog(QWidget *parent = 0); ~Dialog(); CChannel *p1; CChannel *p2; CChannel *p3;private: Ui::Dialog *ui;private slots: void on_pushButton_clicked();};//实现:Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog){ ui->setupUi(this); QVBoxLayout *pLayout = new QVBoxLayout; p1 = new CChannel(this); p2 = new CChannel(this); p3 = new CChannel(this); pLayout->setSpacing(0); pLayout->addWidget(p1); pLayout->addWidget(p2); pLayout->addWidget(p3); pLayout->addStretch(); setLayout(pLayout);}Dialog::~Dialog(){ delete ui;}void Dialog::on_pushButton_clicked(){ QRect rect1,rect2,rect3; int x1,x2,x3,y1,y2,y3; int ttt; rect1 = p1->geometry(); rect2 = p2->geometry(); rect3 = p3->geometry(); x1 = rect1.x(); y1 = rect1.y(); x2 = rect2.x(); y2 = rect2.y(); x3 = rect3.x(); y3 = rect3.y(); qDebug()<<x1<<","<<y1; qDebug()<<x2<<","<<y2; qDebug()<<x3<<","<<y3;}