1. qt载入qss文件,qss载入,qt风格文件的载入,外部载入qss
QString MainWindow::loadStyleSheetQString(const QString &sheetName)
{
QFile file(sheetName);
if(!file.open(QFile::ReadOnly)){
QMessageBox::critical(this, tr("error!"), tr("Load style string failed"));
}
QString styleSheet = QString::fromLatin1(file.readAll());
return styleSheet;
}
2. qt slider设置步进,qt拖动条设置步进:
ui->Slider_Duty1_4->setSingleStep(10);
3. qt多线程用法,qt concurrent用法,qt多线程例子,qt启动多线程,QtConcurrent简易多线程:
future = QtConcurrent::run(pISys, &ISysWin::get_all_win_info_loop);
future.cancel();
future.waitForFinished();
4. qt QSetting用法,qsetting用法,qsetting示例,qsetting读写注册表,qsetting注册表例子
QSettings settings(QSettings::SystemScope, "DemoAPP", "12345678-2017-1113-2020-000000001070");
if( settings.value("12345678-2017-1113-2020-aaaaaaaaaaaa/flag",0).toInt() != 1024 ){
qDebug("[%s]%d: flag is not 1024, first time!", __FUNCTION__, __LINE__);
settings.setValue("12345678-2017-1113-2020-aaaaaaaaaaaa/flag", 1024);
settings.setValue("12345678-2017-1113-2020-aaaaaaaaaaaa/encryption", 0x00);
settings.setValue("12345678-2017-1113-2020-aaaaaaaaaaaa/DirtyFlag", 0);
settings.setValue("12345678-2017-1113-2020-aaaaaaaaaaaa/fansMode", 1);
}
if(ui->tabWidget->currentWidget()->hasFocus()){
if(ui->tabWidget->currentIndex()-1 < 0){
ui->tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
ui->tabWidget->currentWidget()->setFocus();
}else{
ui->tabWidget->setCurrentIndex(ui->tabWidget->currentIndex()-1);
ui->tabWidget->currentWidget()->setFocus();
}
}
6. windows上qt单例程序运行的方法,单个程序运行:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HANDLE proSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 proEntry;
memset(&proEntry, 0, sizeof(PROCESSENTRY32));
proEntry.dwSize = sizeof(PROCESSENTRY32);
bool exist = Process32First(proSnap, &proEntry);
int count = 0;
while(exist)
{
QString file = QString::fromWCharArray(proEntry.szExeFile);
if (QApplication::applicationName()+".exe" == file)
count++;
exist = Process32Next(proSnap, &proEntry);
}
CloseHandle(proSnap);
if (count >= 2){
return 0;
}
MainWindow w;
w.show();
return a.exec();
}
7. qt代码设置style sheet,qt设置style sheet,setStyleSheet:
ui->Slider_Duty1_2->setStyleSheet("QSlider::groove::horizontal{\
border: 1px solid #5F5D5D;\
background: #332C2B;\
height: 8px;\
}\
QSlider::handle:horizontal {\
border: 1px solid #5F5D5D;\
width: 16px;\
height: 16px;\
background-color: #c0c0c0;\
margin: -4px -8px -4px -8px;\
}\
QSlider::sub-page:horizontal{\
background: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 #b2b2b2, stop:0.25 #b2b2b2, stop:0.5 #d5d5d5, stop:1 #d5d5d5);\
height: 8px;\
}");
8. 基于Windows mingw的qt如何加入外部库文件,qt库文件加入,qt外部库:
LIBS += -LE:\testing_projects\QtProjects\QtAdvaningGenePrj\ -lhidapi
LIBS += -LE:\testing_projects\QtProjects\QtAdvaningGenePrj\ -lPsapi
9. qt创建右下角图标,qt系统托盘的创建,qt系统图标,qt程序图标创建代码,qt程序托盘图标示例:
void MainWindow::create_tray_menu(void)
{
miniSizeAction = new QAction(tr("最小化(&N)"),this);
miniSizeAction->setToolTip(tr("最小化"));
miniSizeAction->setIcon(QIcon(":/ico_0.png"));
restoreWinAction = new QAction(tr("还原(&R)"),this);
restoreWinAction->setToolTip(tr("还原"));
restoreWinAction->setIcon(QIcon(":ico_0.png"));
quitAction = new QAction(tr("退出(&Q)"),this);
quitAction->setToolTip(tr("退出"));
quitAction->setIcon(QIcon(":/ico_1.png"));
checkUpdate = new QAction(tr("检查更新(&C)"),this);
checkUpdate->setToolTip(tr("检查更新"));
checkUpdate->setIcon(QIcon(":/ico_2.png"));
connect(miniSizeAction,SIGNAL(triggered()),this,SLOT(hide()));
connect(restoreWinAction,SIGNAL(triggered()),this,SLOT(showNormal()));
connect(quitAction,SIGNAL(triggered()),this,SLOT(exit_program()));
connect(checkUpdate,SIGNAL(triggered()),this,SLOT(checkSysUpdate()));
myMenu = new QMenu((QWidget*)QApplication::desktop());
myMenu->addAction(miniSizeAction);
myMenu->addAction(restoreWinAction);
myMenu->addSeparator();
myMenu->addAction(checkUpdate);
myMenu->addSeparator();
myMenu->addAction(quitAction);
}
void MainWindow::create_tray_icon(void)
{
create_tray_menu();
if(!QSystemTrayIcon::isSystemTrayAvailable())
{
return;
}
myTrayIcon = new QSystemTrayIcon(this);
myTrayIcon->setIcon(QIcon(":/ico_0.png"));
myTrayIcon->setToolTip(tr("ADVGENEV1.07"));
myTrayIcon->showMessage("AdvGene","软件进入了最小化模式,若想恢复,点击还原",QSystemTrayIcon::Information,10000);
myTrayIcon->setContextMenu(myMenu);
myTrayIcon->show();
connect(myTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconAcitvated(QSystemTrayIcon::ActivationReason)));
}
使用方法: 直接调用create_tray_icon即可
10. windows开关机时间,windows捕抓事件,windows休眠事件,win开机事件:
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(result);
Q_UNUSED(eventType);
MSG *msg = static_cast<MSG*>(message);
if(msg->message == WM_QUERYENDSESSION){
check_usb_staus_timer->stop();
pRawUsbData->DirtyFlag++;
pRawUsbData->encryption=23;
pAdvGeneHid->hid_send_data_HID_struct(pRawUsbData);
}else if(msg->message == WM_POWERBROADCAST && msg->wParam == PBT_APMRESUMEAUTOMATIC){
pRawUsbData->DirtyFlag++;
pRawUsbData->encryption=24;
pAdvGeneHid->hid_send_data_HID_struct(pRawUsbData);
check_usb_staus_timer->start(533);
}else if(msg->message == WM_POWERBROADCAST && msg->wParam == PBT_APMSUSPEND){
check_usb_staus_timer->stop();
pRawUsbData->DirtyFlag++;
pRawUsbData->encryption=23;
pAdvGeneHid->hid_send_data_HID_struct(pRawUsbData);
}
return QMainWindow::nativeEvent(eventType, message, result);
}