1、Qt无边框窗体、最小化、任务栏右键:
- 创建“Qt Gui 应用”工程,类信息继承自QWidget;
- 在构建函数里继承QWidget类或者QDialog类,QWflags填写为Qt::FramelessWindowHint就行;
(
class
CreGuiCreator
:
public
QWidget
)
CreGuiCreator::
CreGuiCreator(
QWidget
*parent)
:
//
第一个参数是设置无边框。第二个参数是允许任务栏按钮右键菜单,第三个参数是允许最小化与还原。
QWidget(parent,
Qt::
FramelessWindowHint
|
Qt::
WindowSystemMenuHint
|
Qt::
WindowMinimizeButtonHint),
ui(
new
Ui::
CreGuiCreator)
{
ui->setupUi(
this);
//设置窗体标题及大小
this->setWindowTitle(
"视界组态");
this->setFixedSize(
1920,
1080);
}
//
设置窗体标题栏隐藏并设置位于顶层
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
2、QWidget居中显示:
//this为QWidget对象
QDesktopWidget*
desktop
=
QApplication::desktop();
move((desktop->width()
-
this->width())/
2,
(desktop->height()
-
this->height())/
2);
3、Qt中文乱码:
int
main(
int
argc,
char
*argv[])
{
QApplication
a(argc,
argv);
//设置窗体编码格式
QTextCodec
*pCodec
=
QTextCodec::codecForName(
"utf8");
QTextCodec::setCodecForLocale(pCodec);
QTextCodec::setCodecForCStrings(pCodec);
QTextCodec::setCodecForTr(pCodec);
CreGuiCreator
w;
w.show();
return
a.exec();
}
4、设置窗体背景:
int
main(
int
argc,
char
*argv[])
{
QApplication
a(argc,
argv);
//设置窗体编码格式
QTextCodec
*pCodec
=
QTextCodec::codecForName(
"utf8");
QTextCodec::setCodecForLocale(pCodec);
QTextCodec::setCodecForCStrings(pCodec);
QTextCodec::setCodecForTr(pCodec);
CreGuiCreator
w;
//设置窗体背景色
QPalette
palette
=
w.palette();
palette.setColor(
QPalette::
Background,
QColor(
192,
253,
123));
w.setPalette(palette);
w.setAutoFillBackground(
true);
w.show();
return
a.exec();
}
4、设置窗体底图,当图片存在中空时,透明显示背景色:
void
CreGuiCreator::
paintEvent(
QPaintEvent
*)
{
QPainter
painter(
this);
QString
sRunPath
=
QCoreApplication::applicationDirPath();
sRunPath
+=
"/图片images/bg_flag.png";
QPixmap
pixMap(sRunPath);
painter.drawPixmap(
0,
0,
pixMap);
}
5、窗体大小铺满屏幕:
CreGuiCreator
w;
//获取设备屏幕大小
QDesktopWidget*
pDesktopW
=
QApplication::desktop();
QRect
screenRect
=
pDesktopW->screenGeometry();
int
iWidth
=
screenRect.width();
int
iHeight
=
screenRect.height();
w.setFixedSize(iWidth,
iHeight);
//调试信息输出
QString
sInfo
=
"iWidth="
+
QString::number(iWidth,
10)
+
",iHeight="
+
QString::number(iHeight,
10);
qDebug(sInfo.toLocal8Bit());
6、扣取图片的某一区域在底图上重叠显示:
QPainter
painter(
this);
QString
sRunPath
=
QCoreApplication::applicationDirPath();
QString
src1
=
sRunPath
+
"/图片
images/stop.png";
QString
src2
=
sRunPath
+
"/图片
images/alarm.png";
QPixmap
pixMap(src1);
painter.drawPixmap(
0,
0,
pixMap);
//绘制底图
QRectF
target(
500,
180,
310,
150);
//建立目标矩形
QRectF
source(
500,
180,
310,
150);
//建立源矩形,用来框定源图像文件中要显示的部分
QImage
image(src2);
//建立QImage类对象image
painter.drawImage(target,
image,
source);
//将源图像文件中source区域的图像画到painter
device中的target区域上,大小自适应
7、绘制中间透明的矩形框:
QPainter
*painter;
painter->setPen(
QPen(
Qt::
red));
painter->setBrush(
QBrush(
Qt::
transparent,
Qt::
SolidPattern));
painter->drawRect(
mStartX,
mStartY,
mStopX
-
mStartX,
mStopY
-
mStartY);
8、弹出提示对话框:
QMessageBox
myMsgBox(
this);
myMsgBox.setText(tr(
"是否保存已修改?"));
QPushButton*
pOkBtn
=
myMsgBox.addButton(tr(
"保存"),
QMessageBox::
ActionRole);
myMsgBox.addButton(tr(
"不保存"),
QMessageBox::
ActionRole);
myMsgBox.exec();
if(pOkBtn
==
myMsgBox.clickedButton())
{
.......
}