void MainWindow::closeEvent(QCloseEvent *event) { if (maybeSave()) { writeSettings(); event->accept(); } else { event->ignore(); } }
QString fileName = QFileDialog::getOpenFileName(this); if (!fileName.isEmpty()) { loadFile(fileName); }
QString fileName = QFileDialog::getSaveFileName(this); if (fileName.isEmpty()) { return false; }
newAct = new QAction(QIcon(":/imagess/new.png"), tr("&New"), this); newAct->setShortcut(tr("Ctrl+N")); newAct->setStatusTip(tr("Create a new file")); connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); openAct = new QAction(QIcon(":/imagess/open.png"), tr("&Open..."), this); openAct->setShortcut(tr("Ctrl+O")); openAct->setStatusTip(tr("Open an existing file")); connect(openAct, SIGNAL(triggered()), this, SLOT(open())); saveAct = new QAction(QIcon(":/imagess/save.png"), tr("&Save"), this); saveAct->setShortcut(tr("Ctrl+S")); saveAct->setStatusTip(tr("Save the document to disk")); connect(saveAct, SIGNAL(triggered()), this, SLOT(save())); saveAsAct = new QAction(tr("Save &As..."), this); saveAsAct->setStatusTip(tr("Save the document under a new name")); connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setStatusTip(tr("Exit the application")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); cutAct = new QAction(QIcon(":/imagess/cut.png"), tr("Cu&t"), this); cutAct->setShortcut(tr("Ctrl+X")); cutAct->setStatusTip(tr("Cut the current selection's contents to the " "clipboard")); connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut())); copyAct = new QAction(QIcon(":/imagess/copy.png"), tr("&Copy"), this); copyAct->setShortcut(tr("Ctrl+C")); copyAct->setStatusTip(tr("Copy the current selection's contents to the " "clipboard")); connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy())); pasteAct = new QAction(QIcon(":/imagess/paste.png"), tr("&Paste"), this); pasteAct->setShortcut(tr("Ctrl+V")); pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current " "selection")); connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste())); aboutAct = new QAction(tr("&About"), this); aboutAct->setStatusTip(tr("Show the application's About box")); connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); aboutQtAct = new QAction(tr("About &Qt"), this); aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAct); fileMenu->addAction(openAct); fileMenu->addAction(saveAct); fileMenu->addAction(saveAsAct); fileMenu->addSeparator(); fileMenu->addAction(exitAct); editMenu = menuBar()->addMenu(tr("&Edit")); editMenu->addAction(cutAct); editMenu->addAction(copyAct); editMenu->addAction(pasteAct); menuBar()->addSeparator(); helpMenu = menuBar()->addMenu(tr("&Help")); helpMenu->addAction(aboutAct); helpMenu->addAction(aboutQtAct);
fileToolBar = addToolBar(tr("File")); fileToolBar->addAction(newAct); fileToolBar->addAction(openAct); fileToolBar->addAction(saveAct); editToolBar = addToolBar(tr("Edit")); editToolBar->addAction(cutAct); editToolBar->addAction(copyAct); editToolBar->addAction(pasteAct);
QSettings settings("Trolltech", "Application Example"); QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint(); QSize size = settings.value("size", QSize(400, 400)).toSize();
QSettings settings("Trolltech", "Application Example"); settings.setValue("pos", pos()); settings.setValue("size", size());
int ret = QMessageBox::warning(this, tr("Application"), tr("The document has been modified.\n" "Do you want to save your changes?"), QMessageBox::Yes | QMessageBox::Default, QMessageBox::No, QMessageBox::Cancel | QMessageBox::Escape); if (ret == QMessageBox::Yes) return save(); else if (ret == QMessageBox::Cancel) return false;
win32:CONFIG += console
./configure -static //一定要加static选项 gmake gmake install
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK")); QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK")); QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
class MyThread : public QThread { public: void run(); }; void MyThread::run() { QProcess::execute("notepad.exe"); }
class ............... {........... MyThread thread; ............ }; ..................... thread.start();
void myMessageOutput( QtMsgType type, const char *msg ) { switch ( type ) { case QtDebugMsg: //写入文件; break; case QtWarningMsg: break; case QtFatalMsg: abort(); } } int main( int argc, char** argv ) { QApplication app( argc, argv ); qInstallMsgHandler( myMessageOutput ); ...... return app.exec(); }
<!DOCTYPE RCC><RCC version="1.0"> <qresource> <file>imagess/copy.png</file> <file>imagess/cut.png</file> <file>imagess/new.png</file> <file>imagess/open.png</file> <file>imagess/paste.png</file> <file>imagess/save.png</file> </qresource> </RCC>
RESOURCES = res.qrc
... :imagess/copy.png ...
#ifndef IMAGEWIDGET_HPP #define IMAGEWIDGET_HPP #include <QtCore> #include <QtGui> class ImageWidget : public QWidget { Q_OBJECT public: ImageWidget(QWidget *parent = 0, Qt::WindowFlags f = 0); virtual ~ImageWidget(); protected: void resizeEvent(QResizeEvent *event); private: QImage _images; }; #endif CPP文件: ImageWidget.cpp 引用 #include "ImageWidget.hpp" ImageWidget::ImageWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) { _images.load("images/images_background"); setAutoFillBackground(true); // 这个属性一定要设置 QPalette pal(palette()); pal.setBrush(QPalette::Window, QBrush(_images.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); setPalette(pal); } ImageWidget::~ImageWidget() { } // 随着窗体变化而设置背景 void ImageWidget::resizeEvent(QResizeEvent *event) { QWidget::resizeEvent(event); QPalette pal(palette()); pal.setBrush(QPalette::Window, QBrush(_images.scaled(event->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); setPalette(pal); }
答:在你的HTML网页代码的HTML元素节点上增加一个属性contenteditable就可以使QWebView中查看的网页进入编辑状态了。
转自:http://www.qtcn.org/bbs/read-htm-tid-1324.html