QT 定时关机、共享内存、启动浏览器、浏览器前进后退刷新、进度条、设置浏览器标题、QML入门
生活随笔
收集整理的這篇文章主要介紹了
QT 定时关机、共享内存、启动浏览器、浏览器前进后退刷新、进度条、设置浏览器标题、QML入门
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
定時(shí)關(guān)機(jī)
.h #include <QTimer> #include <stdlib.h> #include <QByteArray>//構(gòu)造函數(shù) //system("shutdown -s -t 7200"); QString cmd = :shutdown -s -t: cmd+= ..... system(cmd.toAssic().data());QTimer *timer = new QTimer; connect(timer, SIGNAL(timeout()), this, SLOT(close())); timer-start(5*1000);?
共享內(nèi)存
.h #include <QShareMemory> #include <QFileDialog> #include <QPixmap> #include <QBuffer> #include <QDataStream> QShareMemory *memory;void MainWindow::loadFromFileSlot() {//判斷當(dāng)前進(jìn)程是u否存在共享內(nèi)存if (this->memory->isAttached())this->memory->detach(); //刪除QString fileName = QFileDialog::openFileName(this, "Open Image", QString(), tr("Image(*.png,*.bmp,*.jpg)"));//QImage load方法將y一個(gè)圖片文件的名稱加載圖片對象當(dāng)中QImage image;if (!Image.load(fileName)){return;}//QBuffer 他是一個(gè)文件讀寫的界面,但是文件是在內(nèi)存當(dāng)中//QTextStream in()QBuffer buffer; //既然是一個(gè)文件 就可以對他進(jìn)行讀寫bool ok = buffer.open(QIODivice::ReadWrite);if (ok){QTextStream in(&buffer);in << image;int size = buffer.size(); //獲取寫入數(shù)據(jù)的大小(圖片的大小)qDebug()<<"緩沖區(qū)的大小為:"<<size;qDebug()<<sharememory->key();qDebug()<<sharememory->nativeKey();//sharememory->setKey("共享內(nèi)存"); //修改共享內(nèi)存的關(guān)鍵字,將無法訪問共享內(nèi)存qDebug()<<sharememory->key();qDebug()<<sharememory->nativeKey();qDebug()<<sharememory->error();qDebug()<<sharememory->errorString();else{return;} }void MainWindow::loadFormMemorySlot() {if (!sharememory->attach()) //關(guān)聯(lián)共享內(nèi)存{ui->Label_Display->setText("無法關(guān)聯(lián)共享內(nèi)存");return;}QBuffer buffer; //構(gòu)建緩沖區(qū)QDataStream out(&buffer); //建立數(shù)據(jù)流對象,并和緩沖區(qū)關(guān)聯(lián)QImage image;//對共享內(nèi)存進(jìn)行讀寫操作sharememory->lock(); //鎖定共享內(nèi)存//初始化緩沖區(qū)中的數(shù)據(jù),setData函數(shù)用來初始化緩沖區(qū)。//該函數(shù)如果在open()函數(shù)之后被調(diào)用,則不起任何作用。//buffer.open(QBuffer::ReadOnly); //解除注釋,則setData函數(shù)不起作用,無法加載內(nèi)存中數(shù)據(jù)buffer.setData((char*)sharememory->constData(), sharememory->size());buffer.open(QBuffer::ReadOnly); //只讀方式打開緩沖區(qū)out >> image; //將緩沖區(qū)的數(shù)據(jù)寫入QImage對象sharememory->unlock(); //釋放共享內(nèi)存sharememory->detach(); //解除程序和共享內(nèi)存的關(guān)聯(lián)ui->Label_Display->setPixmap(QPixmap::fromImage(image)); //顯示圖片 }?
啟動瀏覽器
#include <QtWebKit/QtWebKit> #include <QtWebKit/QWebView> #include <QUrl>void MainWindow::on_showButton_clickedO() {QWebView *view = new QWebView;view->load(QUrl("http://www.zhaoyun.com"));view->show(); }?
瀏覽器前進(jìn)后退刷新
QObject::connect(ui->linEdit, SIGNAL(returnPressed(), this, SLOT(AAA())));void AAA() {ui->webView->load(QUrl(ui->lineEdit->text())); }?
進(jìn)度條
#include <QProcessBar>.h QProcessBar *progress;//構(gòu)造函數(shù) this->progress = new QProcessBar; ui->statusBar->addWidget(this->progress); QObject::connect(ui->webView, SIGNAL(urlChanged(Qurl), this->progress, SLOT(setValue(int)))); QObject::connect(ui->webView, SIGNAL(loadFinished(bool), this->progress, SLOT(deleteProcessBarSlot(bool))));//銷毀進(jìn)度條 void MainWindow::deleteProcessBarSlot(bool ok) {if (ok){delete this->progress;this->progress = NULL;this->ui->statusBar->showMessgae("Load Finished", 5*1000);} }?
設(shè)置瀏覽器標(biāo)題
OBject::connect(ui->webView, SIGNAL(titleChanged(QString)), this, SLOT(setTitleSlot(QString)));void MainWindow::setTitleSlot(QString str) {this->setWindowTitle(str); }?
QML入門
import QtQuick 1.1Rectangle {width: 360height: 360color:"green"Text {text: qsTr("Hello World")color:"red"anchors.centerIn: parent}MouseArea {anchors.fill: parentonClicked: {Qt.quit();}} }?
總結(jié)
以上是生活随笔為你收集整理的QT 定时关机、共享内存、启动浏览器、浏览器前进后退刷新、进度条、设置浏览器标题、QML入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Z-Stack Home Develop
- 下一篇: linux centOS基本配置搭建