首页 > 代码库 > Qt学习1
Qt学习1
Action 的用法
首先在头文件的 private 中加:
1 2 3 | QMenu *fileMenu; QMenu *editMenu; QMenu *helpMenu; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void MainWindow::createActions() //! [17] //! [18] { newAct = new QAction(QIcon( ":/images/new.png" ), tr( "&New" ), this ); newAct->setShortcuts(QKeySequence::New); newAct->setStatusTip(tr( "Create a new file" )); connect(newAct, SIGNAL(triggered()), this , SLOT(newFile())); //! [19] openAct = new QAction(QIcon( ":/images/open.png" ), tr( "&Open..." ), this ); openAct->setShortcuts(QKeySequence::Open); openAct->setStatusTip(tr( "Open an existing file" )); connect(openAct, SIGNAL(triggered()), this , SLOT(open())); } |
menubar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | fileMenu = menuBar()->addMenu(tr( "&File" )); fileMenu->addAction(newAct); //! [28] fileMenu->addAction(openAct); //! [28] fileMenu->addAction(saveAct); //! [26] fileMenu->addAction(saveAsAct); fileMenu->addSeparator(); fileMenu->addAction(exitAct); editMenu = menuBar()->addMenu(tr( "&Edit" )); editMenu->addAction(cutAct); editMenu->addAction(copyAct); editMenu->addAction(pasteAct); |
toolbar
1 2 3 4 5 6 7 8 9 10 11 | fileToolBar = addToolBar(tr( "File" )); fileToolBar->addAction(newAct); //! [29] //! [31] fileToolBar->addAction(openAct); //! [31] fileToolBar->addAction(saveAct); editToolBar = addToolBar(tr( "Edit" )); editToolBar->addAction(cutAct); editToolBar->addAction(copyAct); editToolBar->addAction(pasteAct); |
Qsetting
1 2 3 | QSettings settings( "QtProject" , "Application Example" ); settings.setValue( "pos" , pos()); settings.setValue( "size" , size()); |
1 2 3 | QSettings settings( "QtProject" , "Application Example" ); QPoint pos = settings.value( "pos" , QPoint(200, 200)).toPoint(); QSize size = settings.value( "size" , QSize(400, 400)).toSize(); |
打开文本文件
1 2 3 4 5 6 7 8 9 | void MainWindow::open() //! [7] //! [8] { if (maybeSave()) { QString fileName = QFileDialog::getOpenFileName( this ); if (!fileName.isEmpty()) loadFile(fileName); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | void MainWindow::loadFile( const QString &fileName) //! [42] //! [43] { QFile file(fileName); if (!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning( this , tr( "Application" ), tr( "Cannot read file %1:\n%2." ) .arg(fileName) .arg(file.errorString())); return ; } QTextStream in(&file); #ifndef QT_NO_CURSOR QApplication::setOverrideCursor(Qt::WaitCursor); #endif textEdit->setPlainText(in.readAll()); #ifndef QT_NO_CURSOR QApplication::restoreOverrideCursor(); #endif setCurrentFile(fileName); statusBar()->showMessage(tr( "File loaded" ), 2000); } |
文本文件保存
1 2 3 4 5 6 7 8 9 | bool MainWindow::saveAs() //! [11] //! [12] { QString fileName = QFileDialog::getSaveFileName( this ); if (fileName.isEmpty()) return false ; return saveFile(fileName); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | bool MainWindow::saveFile( const QString &fileName) //! [44] //! [45] { QFile file(fileName); if (!file.open(QFile::WriteOnly | QFile::Text)) { QMessageBox::warning( this , tr( "Application" ), tr( "Cannot write file %1:\n%2." ) .arg(fileName) .arg(file.errorString())); return false ; } QTextStream out(&file); #ifndef QT_NO_CURSOR QApplication::setOverrideCursor(Qt::WaitCursor); #endif out << textEdit->toPlainText(); #ifndef QT_NO_CURSOR QApplication::restoreOverrideCursor(); #endif setCurrentFile(fileName); statusBar()->showMessage(tr( "File saved" ), 2000); return true ; } |
来自为知笔记(Wiz)
Qt学习1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。