首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

自定义Widget的有关问题

2012-03-28 
自定义Widget的问题C/C++ code//头文件:class CChannel : public QWidget{Q_OBJECTpublic:explicit CChann

自定义Widget的问题

C/C++ code
//头文件: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(){}


自定义widget的使用

C/C++ code
//头文件: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;}

上面是大概的相关代码
运行之后为什么两个自定义widget之间的距离有个固定的值呢?明明在布局的时候规定了setSpacing(0)呀,用debug输出看了下好像是30,请问这是为什么?怎样能让两个widget紧挨着呢?谢谢




[解决办法]
把你的widget放到layout里,然后你的pLayout在添加这些layout试试
[解决办法]
在CChannel的构造函数中加入:pLayout->setContentsMargins(0, 0, 0, 0);

热点排行