• 8906阅读
  • 3回复

如何指定一个控件在窗口中的固定位置 [复制链接]

上一主题 下一主题
离线my253629725
 

只看楼主 倒序阅读 楼主  发表于: 2010-02-18
一个窗口,继承于QWidget,在main函数中设置窗口固定大小,怎样在窗口的指定位置安置一个控件


cpp文件
#include "invoic.h"

invoic::invoic(QWidget *parent, Qt::WFlags flags)
    : QWidget(parent, flags)
{
    _image.load("../invoic.jpg");
    setAutoFillBackground(true);
    label = new QLabel(this);label->setGeometry(100,10,180,40);
    label->setText("q         AAA");
    //label->move(3111,222);
    //label->setGeometry(11110, 60, 20, 10);


    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(label);
    //hLayout->setGeometry(QRect(1110,60,20,10));
    setLayout(hLayout);
    
}

invoic::~invoic()
{

}

void invoic::resizeEvent(QResizeEvent *event){
    QPalette pal(palette());
    pal.setBrush(QPalette::Window,QBrush(_image.scaled(event->size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
    setPalette(pal);
    QWidget::resizeEvent(event);
}


h文件
#ifndef INVOIC_H
#define INVOIC_H

#include <QtGui/QWidget>
#include <QtCore>
#include <QtGui>
//#include "ui_invoic.h"

class invoic : public QWidget
{
    Q_OBJECT

public:
    invoic(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~invoic();

protected:
    void resizeEvent(QResizeEvent *);
private:
    //Ui::invoicClass ui;
    QImage _image;
    QLabel *label;
};

#endif // INVOIC_H

man文件
#include <QtGui/QApplication>
#include <QDesktopWidget>
#include "invoic.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    invoic w;
    w.setFixedSize(1018,428);
    QDesktopWidget *desktop = QApplication::desktop();
    w.move((desktop->width() - w.width()) / 2 ,(desktop->height() - w.height()) / 2);
    w.show();
    return a.exec();
}
[ 此帖被my253629725在2010-02-18 16:35重新编辑 ]
离线my253629725

只看该作者 1楼 发表于: 2010-02-18
解决,重载moveEvent
离线my253629725

只看该作者 2楼 发表于: 2010-02-19
昨天的解决方法是错误的,那样只能指定一个控件的固定位置,如果是多个控件的话只需下面两行代码就可以了
dateEdit->setGeometry(9, 9, 50, 25);
dateEdit->setParent(this);
//该行代码是设置控件的属性,如透明,边角是否是圆的,左右边距
dateEdit->setStyleSheet("background-color: rgba(0,0,0,0);border: 1px groove gray;border-top-left-radius:6px;border-top-right-radius:6px;border-bottom-left-radius:6px;border-bottom-right-radius:6px;padding-left:0px;padding-right:0px;");
    
离线javalover

只看该作者 3楼 发表于: 2010-02-19
学习了!
快速回复
限100 字节
 
上一个 下一个