feisky

云计算、虚拟化与Linux技术笔记
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一些QT基础知识

Posted on 2010-04-07 19:35  feisky  阅读(1308)  评论(0编辑  收藏  举报

 

QDialog:
exec()模态对话框,show()非模态对话框
Public Slots
      virtual void    accept ():    Hides the modal dialog and sets the result code to Accepted.
      virtual void    done ( int r ):    Closes the dialog and sets its result code to r.
      int    exec ()         Shows the dialog as a modal dialog, blocking until the user closes it.
      void    open ()       Shows the dialog as a window modal dialog, returning immediately.
      virtual void    reject ()       Hides the modal dialog and sets the result code to Rejected.     
Signals
    void    accepted ()      This signal is emitted when the dialog has been accepted either by the user or by calling accept() or done() with the QDialog::Accepted argument. Note that this signal is not emitted when hiding the dialog with hide() or setVisible(false). This includes deleting the dialog while it is visible.        
    void    finished ( int result )   This signal is emitted when the dialog's result code has been set, either by the user or by calling done(), accept(), or reject(). Note that this signal is not emitted when hiding the dialog with hide() or setVisible(false). This includes deleting the dialog while it is visible.
    void    rejected ()           This signal is emitted when the dialog has been rejected either by the user or by calling reject() or done() with the QDialog::Rejected argument.   

中文显示乱码的处理:
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));

在后面的话就可以使用中文了。
QMessageBox::warning(this,tr("错误"),tr("用户名或密码错误!"),QMessageBox::Yes);


QMessageBox::
        question        For asking a question during normal operations.
        information      For reporting information about normal operations.
        warning          For reporting non-critical errors.
        critical            For reporting critical errors.
        about           Displays a simple about box with title title and text text
        aboutQt       Displays a simple message box about Qt, with the given title and centered


QMessageBox的使用:
        QMessageBox msg;
        msg.setIcon(QMessageBox::Warning);
        msg.setWindowTitle(tr("错误"));
        msg.setText(tr("用户名或密码错误!"));
        msg.setDetailedText(tr("请检查用户名和密码是否输入错误。"));
        msg.setStandardButtons(QMessageBox::Yes);
        if(msg.exec()==QMessageBox::Yes)
            ui->nameEdit->setFocus();
或则像下面这样:
    QMessageBox::warning(this,tr("错误"),tr("用户名或密码错误!"),QMessageBox::Yes);

 

对话框的使用

QFontDialog::getFont
QColorDialog::getColor
QFileDialog::getOpenFileName
QInputDialog::getText

 

设置字体对话框
void Dialog::on_setFontBtn_clicked()
{
    bool ok;
    const QFont& font=QFontDialog::getFont(&ok,ui->textFromFile->font(),this);
    if(ok)
    {
        ui->textFromFile->setFont(font);
    }
}

 

打开文本文件

void Dialog::openFile()
{
    QString filename=QFileDialog::getOpenFileName(this,tr("打开文件"),
                                              tr("e:/"),tr("txt(*.txt)"));
    QFile file(filename);
    if(file.open(QIODevice::ReadOnly))
    {
        QTextStream stream(&file);
        QString line;
        while(!stream.atEnd())
        {
            line=stream.readLine();
            ui->textFromFile->insertPlainText(line);
        }
    }
    file.close();
}

无觅相关文章插件,快速提升流量