常见的几个Qt编程问题的处理

  常见的几个Qt编程问题的处理

1、如果在窗体关闭前自行判断是否可关闭
答:重新实现这个窗体的closeEvent()函数,加入判断操作
QUOTE:

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (maybeSave())
    {
        writeSettings();
        event->accept();
    }
    else
    {
        event->ignore();
    }
}


2、如何用打开和保存文件对话
答:使用QFileDialog
QUOTE:

        QString fileName = QFileDialog::getOpenFileName(this);
        if (!fileName.isEmpty())

        {
              loadFile(fileName);
        }


QUOTE:

    QString fileName = QFileDialog::getSaveFileName(this);
    if (fileName.isEmpty())

    {
        return false;
    }


3、如果创建Actions(可在菜单和工具栏里使用这些Action)
答:
QUOTE:

newAct = new QAction(QIcon(":/images/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(":/images/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(":/images/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(":/images/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(":/images/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(":/images/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()));

4、如果创建主菜单
答:采用上面的QAction的帮助,创建主菜单
QUOTE:

    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);


5、如果创建工具栏
答:采用上面的QAction的帮助,创建工具栏
QUOTE:

    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);

6、如何使用配置文件保存配置
答:使用QSettings类
QUOTE:

    QSettings settings("Trolltech", "Application Example");
    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
    QSize size = settings.value("size", QSize(400, 400)).toSize();


QUOTE:

    QSettings settings("Trolltech", "Application Example");
    settings.setValue("pos", pos());
    settings.setValue("size", size());


7、如何使用警告、信息等对话框
答:使用QMessageBox类的静态方法
QUOTE:

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;


8、如何使通用对话框中文化
答:对话框的中文化
比 如说,QColorDialog的与文字相关的部分,主要在qcolordialog.cpp文件中,我们可以从qcolordialog.cpp用 lupdate生成一个ts文件,然后用自定义这个ts文件的翻译,再用lrelease生成一个.qm文件,当然了,主程序就要改变要支持多国语言了, 使用这个.qm文件就可以了。

另外,还有一个更快的方法,在源代码解开后有一个目录translations,下面有一些.ts, .qm文件,我们拷贝一个:
QUOTE:

cp src/translations/qt_untranslated.ts ./qt_zh_CN.ts

然 后,我们就用Linguist打开这个qt_zh_CN.ts,进行翻译了,翻译完成后,保存后,再用lrelease命令生成qt_zh_CN.qm, 这样,我们把它加入到我们的qt project中,那些系统的对话框,菜单等等其它的默认是英文的东西就能显示成中文了。

9、在Windows下Qt里为什么没有终端输出?
答:把下面的配置项加入到.pro文件中
QUOTE:

win32:CONFIG += console


10、Qt 4 for X11 OpenSource版如何静态链接?
答:编译安装的时候加上-static选项
QUOTE:
./configure -static   //一定要加static选项
gmake
gmake install

然后,在Makefile文件中加 static 选项,就可以连接静态库了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值