Qwidget:所有窗口/控件的基类
QMainWindow:自动添加QmenuBar/QtoolBar/QStatusBar/centralWidget
常用配置
setObjectName("myWidget"); // 名称
setWindowFlags(Qt::FramelessWindowHint // 无边框
| Qt::WindowStaysOnTopHint // 置于顶层
| windowFlags());
setCursor(Qt::BlankCursor); // 隐藏鼠标
setAttribute(Qt::WA_DeleteOnClose,true); // 关闭时自动释放
setAttribute(Qt::WA_X11DoNotAcceptFocus, true);// 无焦点击
setAttribute(Qt::WA_TranslucentBackground); // 设置窗口背景透明
resize(width, height); // 修改大小
setFixedSize(width, height); // 固定大小
showFullScreen(); // 全屏
move(x,y); // 相对于父窗口的位置
setGeometry(x, y, width, height); // 相对于屏幕的位置
setStyleSheet("QWidget#myWidget {}"); // 样式
setScaledContents(true); // 设置图片显示时自动匹配大小
setWindowOpacity(1); // 透明度
背景
1. 通过setStyleSheet()
2. 通过Qpalette
3. 通过paintEvent()
/**
* @description: 设置窗口的背景图
* @param {type}
* @return:
*/
void set_background_image(const QString &imagePath) {
QPixmap pixmap(imagePath);
QPalette palette = this->palette();
palette.setBrush(this->backgroundRole(), QBrush(pixmap.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
this->setPalette(palette);
}
/**
* @description: 设置窗口的背景色
* @param {type}
* @return:
*/
void set_background_image(const QString &imagePath) {
QPixmap pixmap(imagePath);
QPalette palette = this->palette();
// palette.setBrush(QPalette::Background,QBrush(Qt::red));
palette.setColor(QPalette::Background, QColor(255, 255, 255));
this->setPalette(palette);
}
绘制
/**
* @description:
* @param {type}
* @return:
*/
void paintEvent(QPaintEvent *){
QStyleOption option;
option.init(this);
QPainter painter(this);
style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
}
};