Qt判斷當(dāng)前操作系統(tǒng)?
#ifdef Q_OS_MAC //mac
...
#endif
#ifdef Q_OS_LINUX //linux
...
#endif
#ifdef Q_OS_WIN32 //win
...
#endif
#ifdef __arm__ //arm
...
#endif
Qt實(shí)現(xiàn)應(yīng)用程序關(guān)閉和重啟?
//關(guān)機(jī)按鈕-點(diǎn)擊槽函數(shù)
void SystemD::on_shutdownButton_clicked()
{
//關(guān)閉應(yīng)用程序
QCoreApplication::exit();
}
//重啟按鈕-點(diǎn)擊槽函數(shù)
void SystemD::on_rebootButton_clicked()
{
//重啟應(yīng)用程序
qApp->quit();
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
}
Qt實(shí)現(xiàn)Linux下的系統(tǒng)關(guān)機(jī)和重啟?
先使Linux的普通用戶可以在不輸入密碼的情況下,執(zhí)行sudo reboot命令實(shí)現(xiàn)重啟。
//關(guān)機(jī)按鈕-點(diǎn)擊槽函數(shù)
void SystemD::on_shutdownButton_clicked()
{
QProcess::execute("sudo halt"); //UBuntu下執(zhí)行關(guān)機(jī)命令(需要root權(quán)限)
}
//重啟按鈕-點(diǎn)擊槽函數(shù)
void SystemD::on_rebootButton_clicked()
{
QProcess::execute("sudo reboot"); //UBuntu下執(zhí)行重啟命令(需要root權(quán)限)
}
Qt 實(shí)現(xiàn)Windows系統(tǒng)關(guān)機(jī)
第一種關(guān)機(jī)方法
#include <Windows.h>
#include <QProcess>
void ShutDown()
{
QString program = "C:/WINDOWS/system32/shutdown.exe";
QStringList arguments;
arguments << "-s";
QProcess *myProcess = new QProcess();
myProcess->start(program, arguments);
}
第二種關(guān)機(jī)方法
#include <Windows.h>
void ShutDown()
{
system("shutdown -s -t 00");
}
重啟指令:shutdown -r -t xx
注銷指令:shutdown -l -t xx
讓Qt 程序休眠一段時(shí)間的方法
在Qt程序中,我們有時(shí)候會(huì)遇到這樣的需求,比如讓程序暫停(休息、休眠)一段時(shí)間。這里介紹以下幾種方法:
一、阻塞型延時(shí)
阻塞的原理就是:在延時(shí)期間,本線程的事件循環(huán)得不到執(zhí)行。
1、QThread類的sleep()
最簡(jiǎn)單的延時(shí)方法就是使用QThread類的sleep(n)、msleep(n)、usleep(n),這幾個(gè)函數(shù)的不良后果就是,GUI會(huì)在延時(shí)的時(shí)間段內(nèi)失去響應(yīng),界面卡死,所以,這三個(gè)函數(shù)一般只用在非GUI線程中。
QThread::sleep(5000);
2、使用定時(shí)器:死等
QTime timer = QTime::currentTime().addMSecs(5000);
while( QTime::currentTime() < timer ); //等待時(shí)間流逝5秒鐘
這樣做會(huì)存在一個(gè)問題,當(dāng)在死循環(huán)的時(shí)候,我們的界面是無法刷新,用戶是不會(huì)響應(yīng)用戶的任何交互的。也就是讓用戶感覺程序已經(jīng)是假死狀態(tài)了。
二、非阻塞延時(shí)
原理無非就是利用事件循環(huán),有兩種原理:
1、處理本線程的事件循環(huán)
在等待中,不斷強(qiáng)制進(jìn)入當(dāng)前線程的事件循環(huán),這樣可以把堵塞的事件都處理掉,從而避免程序卡死。
QTime timer = QTime::currentTime().addMSecs(5000);
while( QTime::currentTime() < timer );
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);這條語句能夠使程序在while等待期間,去處理一下本線程的事件循環(huán),處理事件循環(huán)最多100ms必須返回本語句,如果提前處理完畢,則立即返回這條語句。這也就導(dǎo)致了該Delay_MSec函數(shù)的定時(shí)誤差可能高達(dá)100ms。
2、使用子事件循環(huán)
創(chuàng)建子事件循環(huán),在子事件循環(huán)中,父事件循環(huán)仍然是可以執(zhí)行的。
QEventLoop eventloop;
QTimer::singleShot(5000, &eventloop, SLOT(quit())); //創(chuàng)建單次定時(shí)器,槽函數(shù)為事件循環(huán)的退出函數(shù)
eventloop.exec(); //事件循環(huán)開始執(zhí)行,程序會(huì)卡在這里,直到定時(shí)時(shí)間到,本循環(huán)被退出
Qt實(shí)現(xiàn)右鍵菜單
// 初始化動(dòng)作
QAction *newAction = new QAction("新建",this);
// 初始化右鍵菜單
QMenu *rightClickMenu = new QMenu(this);
// 動(dòng)作添加到右鍵菜單
rightClickMenu->addAction(newAction);
rightClickMenu->addSeparator();
rightClickMenu->addAction(ui->exitAction);
// 給動(dòng)作設(shè)置信號(hào)槽
connect(ui->exitAction, &QAction::triggered, this, &MainWindow::on_exitAction_triggered);
// 給控件設(shè)置上下文菜單策略:鼠標(biāo)右鍵點(diǎn)擊控件時(shí)會(huì)發(fā)送一個(gè)void QWidget::customContextMenuRequested(const QPoint &pos)信號(hào)
this->setContextMenuPolicy(Qt::CustomContextMenu);
Qt綁定回車鍵和確定按鈕
輸完密碼在密碼框按回車等同按了確定按鈕的效果:
connect(m_pEditPasswd, SIGNAL(returnPressed()), this, SLOT(EnterSlot()));
注意:回車鍵同是包含鍵盤區(qū)的回車鍵和小鍵盤區(qū)的回車鍵。
Qt打開文件與保存文件
// 打開文件
QString fileName;
fileName = QFileDialog::getOpenFileName(this,"Open File","","Text File(*.txt)");
if(fileName == "")
{
return;
}
else
{
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(this,"error","open file error!");
return;
}
else
{
if(!file.isReadable())
QMessageBox::warning(this,"error","this file is not readable!");
else
{
QTextStream textStream(&file);
while(!textStream.atEnd())
{
ui->textEdit->setPlainText(textStream.readAll());
}
ui->textEdit->show();
file.close();
flag_isOpen = 1;
Last_FileName = fileName;
}
}
}
// 保存文件
QFileDialog fileDialog;
QString fileName = fileDialog.getSaveFileName(this, "save file", "", "Text File(*.txt)");
if(fileName == "")
{
return;
}
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(this,"error","Open File Faile");
return;
}
else
{
QTextStream textString(&file);
QString str = ui->textEdit->toPlainText();
textString << str;
Last_FileContent = str;
file.close();
}
Qt實(shí)現(xiàn)截屏并保存
// 檢查截圖目錄是否存在,若不存在則新建
QString strDir = QCoreApplication::applicationDirPath() + "/screenshot";
QDir dir;
if (!dir.exists(strDir))
{
if(!dir.mkpath(strDir))
QMessageBox::information(this, "Error", "新建截圖目錄失敗!", QMessageBox::Ok);
}
// 截圖并保存
QPixmap pix = this->grab(QRect(0,0,this->width(),this->height()));
QString fileName= QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss") + ".png";//通過時(shí)間命名文件
if(!pix.save(QCoreApplication::applicationDirPath() + "/screenshot/" + fileName, "png"))
{
QMessageBox::information(this, "Error", "保存錯(cuò)誤 !", QMessageBox::Ok);
}
else
{
QMessageBox::information(this, "Grab", "截圖已保存在:安裝目錄\\Screenshot目錄下!", QMessageBox::Ok);
}
QtCreator 屏蔽指定警告
有兩種方法可以屏蔽指定警告。
方法一:
Tools > Options > C++ > Code Model > Clang Code Model > Manage;
創(chuàng)建自己的配置,這里可以復(fù)制一份原來的配置 “Clang-only checks for almost everything (Copy)” ;
在Clang中添加要屏蔽的警告, 例如: -Wno-old-style-cast、-Wno-deprecated-declarations;
確定后選擇剛剛創(chuàng)建的自己的配置。
方法二:
使用如下語句:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
//這里寫出現(xiàn)警告的代碼就能實(shí)現(xiàn)去除警告(代碼寫在這中間)
#pragma clang diagnostic pop
Qt 通過 objectName 查找該控件
在代碼中,動(dòng)態(tài)創(chuàng)建的一些控件,先通過 setObjectName(“XXX”),然后再通過 findChild 方法查找該控件:
QLabel *macLabel = new QLabel(this);
macLabel->setObjectName("mac");
//查找這個(gè)控件的時(shí)候
QLabel *macLabel = yourWidget->findChild<QLabel*>("mac");
qDebug() << macLabel->text();
Qt模擬鼠標(biāo)點(diǎn)擊事件
通過坐標(biāo)模擬鼠標(biāo)事件點(diǎn)擊事件。
關(guān)鍵函數(shù):QWidget::childAt(pos);
其中 pos 是相對(duì)于 QWidget 的坐標(biāo),坐標(biāo)一般有兩種:全局坐標(biāo)和相對(duì)坐標(biāo)。通過 mapToGlobal() 之類的 API 可以轉(zhuǎn)換。文章來源:http://www.zghlxwxcb.cn/news/detail-424681.html
QWidget* child = this->childAt(pos);
QMouseEvent *pressEvent, *releaseEvent;
pressEvent = new QMouseEvent(QEvent::MouseButtonPress, QPoint(0, 0), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
releaseEvent = new QMouseEvent(QEvent::MouseButtonRelease, QPoint(0, 0), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(child, pressEvent);
QApplication::sendEvent(child, releaseEvent);
通過以上代碼,在 this 指向窗口的 pos 位置的控件(一般是 QPushButton)會(huì)接收到 clicked() 事件。文章來源地址http://www.zghlxwxcb.cn/news/detail-424681.html
// 模擬鼠標(biāo)點(diǎn)擊的第二種方法
QTest::mouseClick(child, Qt::LeftButton, Qt::NoModifier, QPoint(0, 0));
// 發(fā)送事件的第二種方法
QCoreApplication::postEvent(child, pressEvent);
QCoreApplication::postEvent(child, releaseEvent);
// 獲取當(dāng)前的焦點(diǎn)widget
QWidget* focus = QWidget::focusWidget();
到了這里,關(guān)于Qt應(yīng)用開發(fā)常用功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!