qt一些常规用方法记录,以便查找-001

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);//qtconcurrent,qt创建多线程,qt启动多线程,qt其他类多线程
future.cancel();//qt创建和结束多线程,qt结束线程
future.waitForFinished();//qt等待多线程结束

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);
    }

5. qt设置tabwidget的焦点,qt tab的焦点设置:

        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);
    ///==========  qt 单例运行 ===========//
    HANDLE proSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);//TH32CS_SNAPPROCESS表示在快照中包含系统中所有的进程。
    PROCESSENTRY32 proEntry;
    memset(&proEntry, 0, sizeof(PROCESSENTRY32));
    proEntry.dwSize = sizeof(PROCESSENTRY32);//初始化结构体大小。没有这一句Process32First()会失败。
    bool exist = Process32First(proSnap, &proEntry);//获得HANDLE中的第一个进程信息,如果成功,返回true,否则false。
    int count = 0;//用于计数的变量。
    while(exist)//读取进程信息。
    {
        QString file = QString::fromWCharArray(proEntry.szExeFile);    //获得当前进程名并转化为QString类型。
        if (QApplication::applicationName()+".exe" == file)    //进行判断,如果同名就计数。
            count++;
        exist = Process32Next(proSnap, &proEntry);    //获取下一个进程信息,成功返回true,否则返回false。
    }

    CloseHandle(proSnap);    //关闭HANDLE。
    if (count >= 2){    //如果本程序已经在运行,再启动程序时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"));

    //maxSizeAction = new QAction(tr("最大化(&X)"),this);
    //maxSizeAction->setToolTip(tr("最大化"));
    //maxSizeAction->setIcon(QIcon(":/ico_2.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(maxSizeAction,SIGNAL(triggered()),this,SLOT(showNormal()));
    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(maxSizeAction);
    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){
          //QMessageBox::information(NULL, "关机", "捕抓到关机或者注销信息,Windows's Quiting", QMessageBox::Ok, QMessageBox::Ok);
          check_usb_staus_timer->stop();
          pRawUsbData->DirtyFlag++;
          pRawUsbData->encryption=23;//sleep mode or shut down;
          pAdvGeneHid->hid_send_data_HID_struct(pRawUsbData);
      }else if(msg->message == WM_POWERBROADCAST && msg->wParam == PBT_APMRESUMEAUTOMATIC){
          //QMessageBox::information(NULL, "休眠唤醒", "捕抓到休眠唤醒信息,Windows power up again", QMessageBox::Ok, QMessageBox::Ok);
          pRawUsbData->DirtyFlag++;
          pRawUsbData->encryption=24;//wake up
          pAdvGeneHid->hid_send_data_HID_struct(pRawUsbData);
          check_usb_staus_timer->start(533);
      }else if(msg->message == WM_POWERBROADCAST && msg->wParam == PBT_APMSUSPEND){
          //QMessageBox::information(NULL, "休眠", "捕抓到系统进入休眠信息,Windows power suspending: "+currentDate, QMessageBox::Ok, QMessageBox::Ok);
          check_usb_staus_timer->stop();
          pRawUsbData->DirtyFlag++;
          pRawUsbData->encryption=23;//sleep mode
          pAdvGeneHid->hid_send_data_HID_struct(pRawUsbData);
      }
      return QMainWindow::nativeEvent(eventType, message, result);
}

你可能感兴趣的:(技术心得以及事项,qt示例,qt,qss,qt多线程,qt事件)