记录整理QT学习要点
控件类:
控件类:
1. Widget
//设置固定窗口大小
w.setFixSize(500,300);
2. QLabel
//设置标签图片
QPixmap pix("图片地址");
ui->label->setPixmap(pix);
3. QPushButton
//设置按钮贴图
QIcon con(“图片文件地址”);
PushButton->setIcon(con);
//设置按钮背景色
PushButton->setStyleSheet(“background:blue”);
4. QLineEdit
//设置输入框字体
QFont f(“仿宋”,14);
LineEdit->setFont(f);
函数类:
函数类:
1. 进程类:QProcess
Process->start(“可执行文件的地址”);
//startDetached()打开文件夹
QString path = QCoreApplication::applicationDirPath();
path.replace("/","\\");//将地址中的"/"替换为"\",因为在Windows下使用的是"\"。
QProcess *process = new QProcess(this);
process->startDetached("explorer "+ path);
2. 消息框类:QMessageBox
QMessageBox::information(this,”窗口标题”,”提示内容”);
3. 槽函数:connect()
a) connect(ui->LineEdit,SIGNAL(returnPressed()),this,SLOT(槽函数))//宏调用函数
b) connect(ui->LineEdit,&QPushButton::clicked ,this,&Widget::槽函数)//取地址调用函数
c) connect(ui->LineEdit,&QPushButton::clicked ,[this](){槽函数})//调用小型函数
4. QCoreApplication类
QCoreApplication::applicationDirPath()
QCoreApplication::applicationFilePath()
5. QStandardPaths类
QString str2 = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
6. windows.h类
//打开需要管理员权限的软件
QString path = QCoreApplication::applicationDirPath();
QString myexe = (path+"/09-HWiNFO_2023.1.1/HWiNFO64.exe");
SHELLEXECUTEINFO sei = {sizeof(SHELLEXECUTEINFO)};
sei.lpVerb = TEXT("runas");
sei.lpFile = myexe.toStdWString().c_str();
sei.nShow = SW_SHOWNORMAL;
if (!ShellExecuteEx(&sei)) ;
7. QScreen类
//获取当前屏幕宽度
QScreen *screen = QGuiApplication::primaryScreen(); //获取主屏幕
QRect r = screen->geometry();
int width = r.width();
8. QT定时器
//1.头文件放变量、声明虚函数重写
int myTimerID;
virtual void timerEvent(QTimerEvent *event);
//2.widget.cpp启动定时器
myTimerID = this->startTimer(1000);
//3. widget.cpp具体虚函数
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId() != myTimerID)
return;
//写事件
}
//4. 结束
this->killTimer(myTimerID);
给exe添加默认桌面图标的方法:
//1.新建一个.rc文件,“新建文件”—“C++ source File”—命名为logo.rc(不能掉了rc后缀)。
//2.然后pro工程文件添加以下内容
#若你已经添加了QT资源文件,以下两条语句只需更改qss名字即可,如果没有则按该名字创建资源文件
#再将icon图标添加到资源
RESOURCES += \
qss.qrc
RESOURCES += \
你的qrc文件的名字.qrc
DISTFILES += \
logo.rc
RC_FILE += logo.rc
学习日志:
第1天:2024-5-9周四,晴
第2天:2024-5-11周六,雨转阴
第3天:2024-5-19周日,晴