QT 定时关机、共享内存、启动浏览器、浏览器前进后退刷新、进度条、设置浏览器标题、QML入门
生活随笔
收集整理的這篇文章主要介紹了
QT 定时关机、共享内存、启动浏览器、浏览器前进后退刷新、进度条、设置浏览器标题、QML入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
定時關機
.h #include <QTimer> #include <stdlib.h> #include <QByteArray>//構造函數 //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);?
共享內存
.h #include <QShareMemory> #include <QFileDialog> #include <QPixmap> #include <QBuffer> #include <QDataStream> QShareMemory *memory;void MainWindow::loadFromFileSlot() {//判斷當前進程是u否存在共享內存if (this->memory->isAttached())this->memory->detach(); //刪除QString fileName = QFileDialog::openFileName(this, "Open Image", QString(), tr("Image(*.png,*.bmp,*.jpg)"));//QImage load方法將y一個圖片文件的名稱加載圖片對象當中QImage image;if (!Image.load(fileName)){return;}//QBuffer 他是一個文件讀寫的界面,但是文件是在內存當中//QTextStream in()QBuffer buffer; //既然是一個文件 就可以對他進行讀寫bool ok = buffer.open(QIODivice::ReadWrite);if (ok){QTextStream in(&buffer);in << image;int size = buffer.size(); //獲取寫入數據的大小(圖片的大小)qDebug()<<"緩沖區的大小為:"<<size;qDebug()<<sharememory->key();qDebug()<<sharememory->nativeKey();//sharememory->setKey("共享內存"); //修改共享內存的關鍵字,將無法訪問共享內存qDebug()<<sharememory->key();qDebug()<<sharememory->nativeKey();qDebug()<<sharememory->error();qDebug()<<sharememory->errorString();else{return;} }void MainWindow::loadFormMemorySlot() {if (!sharememory->attach()) //關聯共享內存{ui->Label_Display->setText("無法關聯共享內存");return;}QBuffer buffer; //構建緩沖區QDataStream out(&buffer); //建立數據流對象,并和緩沖區關聯QImage image;//對共享內存進行讀寫操作sharememory->lock(); //鎖定共享內存//初始化緩沖區中的數據,setData函數用來初始化緩沖區。//該函數如果在open()函數之后被調用,則不起任何作用。//buffer.open(QBuffer::ReadOnly); //解除注釋,則setData函數不起作用,無法加載內存中數據buffer.setData((char*)sharememory->constData(), sharememory->size());buffer.open(QBuffer::ReadOnly); //只讀方式打開緩沖區out >> image; //將緩沖區的數據寫入QImage對象sharememory->unlock(); //釋放共享內存sharememory->detach(); //解除程序和共享內存的關聯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(); }?
瀏覽器前進后退刷新
QObject::connect(ui->linEdit, SIGNAL(returnPressed(), this, SLOT(AAA())));void AAA() {ui->webView->load(QUrl(ui->lineEdit->text())); }?
進度條
#include <QProcessBar>.h QProcessBar *progress;//構造函數 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))));//銷毀進度條 void MainWindow::deleteProcessBarSlot(bool ok) {if (ok){delete this->progress;this->progress = NULL;this->ui->statusBar->showMessgae("Load Finished", 5*1000);} }?
設置瀏覽器標題
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();}} }?
總結
以上是生活随笔為你收集整理的QT 定时关机、共享内存、启动浏览器、浏览器前进后退刷新、进度条、设置浏览器标题、QML入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Z-Stack Home Develop
- 下一篇: HTML day02