Description: Above the code box is the description of the operation, the specific operation inside the code box
Incomplete display of interface fonts during runtime (resolution problem)
//解决办法:在主函数(main.cpp)的QApplication a(argc,argv)前加入一行:
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
Code way to build components
在.h文件中private 声明指针,在.cpp文件中new实例化对象,后可在cpp中以指针方式使用
Layout of components in code (in fact, this step can be done in qt designer)
1.定义QGridLayout/水平/竖直对象a
2.a->addWidget(部件名,0,0) //添加组件,后面是对应组件x,y坐标,都从0开始
QTextEdit sets text storage format
QTextEdit *textLeft = new QTextEdit();
textLeft->setAlignment(Qt::AlignCenter);
/*
* setAlignment为设置中文字对齐方式
* 包括AlignLeft 左对齐
* AlignRight 右对齐
* AlignCenter 中心对齐
* AlignUp 文字与顶部对齐
* AlignBottom 文字与底部对齐
*/
Window display drag bar
QSplitter *splitterRight = new QSplitter();
splitterRight->setOpaqueResize(false); //用于显示拖拽条,flase不显示,true显示
Set the title of the current window (actually, without tr, double quotes are fine. A is the object name of the current interface)
A->setWindowTitle(tr("Splitter"));
Display the current window (A is the object name of the current interface)
A->show();
Close the current window (A is the object name of the current interface, if it is used in your own CPP file, you can use this)
A->close();;
Set up a movable window
QDockWidget *dock = new QDockWidget(tr("DockWindow1"), this);
//可移动
dock->setFeatures(QDockWidget ::DockWidgetMovable);
dock->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
Set Label to mosaic type
OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);//设置控件的风格,镶嵌阴影型
Reference images as controls (such as photos)
HeadIconLabel = new QLabel;
QPixmap icon("123.jpg");//引用图像时可放置于build目录下
HeadIconLabel->setPixmap(icon);
Code to adjust the size of the control (a is any control object)
a->resize(x,y);
If you need to display the drawer effect, each drawer is divided into N buttons (such as QQ chat list)
QToolBox
Various parameter settings of setToolButtonStyle in QToolButton
/*
* setToolButtonStyle用来设置图标和文字的位置
* 包括:
* ToolButtonIconOnly 只显示图标
* ToolButtonTextOnly 只显示文字
* ToolButtonTextBesidesIcon 文字在图标旁边
* ToolButtonTextUnderIcon 文字在图标下面
*/
toolBtn1_1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
Design a progress box (like the progress bar under windows when deleting large files)
QProgressDialog
Set the window to the top (A is the name of the interface object)
A->setWindowFlags(Qt::FramelessWindowHint);
Set window transparency (A is the name of the interface object)
//设置透明度为0.5
A->setWindowOpacity(0.5);
Mouse movement event
//调用时鼠标移动使用的应该是->buttons()而不是->button
//以下两个函授完成时钟随着鼠标移动的功能
void DigiClock::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton)
{
//以左上角为标准计算拖动距离
//globalPos代表全局坐标,以整个屏幕为界面显示
dragPosition = event->globalPos()-frameGeometry().topLeft();
event->accept();
}
if (event->buttons() == Qt::RightButton)
{
close();
}
}
void DigiClock::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton)
{
move(event->globalPos()-dragPosition);
event->accept();
}
}
Button: Press to change color, and restore after release (implemented using QTime)
//(注意:基本思想是修改样式表的背景,所以要保证前后字体大小统一。样式表的话推荐选择border-image,background的图案会显示不全)
this->ui->P_back->setStyleSheet("QPushButton{border-image: url(:/tubiao/14.png);color: rgb(255, 255, 255);font: 14pt 楷体;}");
//使用等待效果,让图标变换有显示
QTime dieTime= QTime::currentTime().addMSecs(300);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
this->close();
this->ui->P_back->setStyleSheet("QPushButton{border-image: url(:/tubiao/10.png);color: rgb(255, 255, 255);font: 14pt 楷体;}");
Read the data in the TXT file
int i = 1;
QFile file("xxx.txt"); //文件路径,最好是绝对路径
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<"Can't open the file!";
return;
}
QTextStream in(&file); //文件流
QString line = in.readLine() ; //读取一行
while (!line.isNull())
{
line = in.readLine();
a[i] = line.toFloat();
i = i+1;
}
Current clock, dynamic change
//主函数
QTimer *timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(showtime_1()));
timer->start(1000);//1s改变一次
//槽函数showtime_1
QDateTime *datatime = new QDateTime(QDateTime::currentDateTime());
QString str = datatime->toString("yyyy-MM-dd hh:mm:ss ddd");
ui->D_time->setText(str);