QMainWindow学习-6

现在我们的按钮还不支持复制,粘贴这些操作,那我们接下来做这些事情。
首先我们将剪切和复制两个按钮设置成不可见
cutAct->setEnabled(false);
copyAct->setEnabled(false);
然后加上两个连接
connect(textEdit, SIGNAL(copyAvailable(bool)), cutAct,
SLOT(setEnabled(bool)));
connect(textEdit, SIGNAL(copyAvailable(bool)), copyAct, SLOT(setEnabled(bool)));
这两个什么意思呢,当我们用鼠标选择了一段文本时,那么这个copyAvailable信号发出,表示可以复制剪切了,同时这个信号有一个参数,可以复制为true,可以复制了当然要将这两个按钮设置成可见状态,这样用户才能做操作。
然后再来三个连接,当用户点击这些按钮时,执行相应的复制、剪切和粘贴操作。
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));

同样的,我们写undo和redo操作
undoAct->setEnabled(textEdit->document()->isUndoAvailable());
redoAct->setEnabled(textEdit->document()->isRedoAvailable());
connect(textEdit->document(), SIGNAL(undoAvailable(bool)), undoAct, SLOT(setEnabled(bool)));
connect(textEdit->document(), SIGNAL(redoAvailable(bool)), redoAct, SLOT(setEnabled(bool)));
同样要设置这两个的可见状态,然后发生了点击操作时,执行相应的操作。
connect(undoAct, SIGNAL(triggered()), textEdit, SLOT(undo()));
connect(redoAct, SIGNAL(triggered()), textEdit, SLOT(redo()));

导出为pdf文件
定义槽,写上连接
void MainWindow::exportPdf()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Export Pdf"),
                                                    QString(), "*.pdf");
    if (!fileName.isEmpty()) {
        if (QFileInfo(fileName).suffix().isEmpty()) {
            fileName.append(".pdf");
        }
        QPrinter printer(QPrinter::HighResolution);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName(fileName);
        textEdit->document()->print(&printer);
    }
}
还是获取导出pdf的名字,调用QPrinter类,设置它为pdf格式,和输出的名字,然后将文本输出到这个文件上。

posted @ 2011-11-25 17:18  移动应用开发  阅读(192)  评论(0编辑  收藏  举报