Qt学习笔记01_标准对话框


Dialog:对话框:
    按照运行时该对话框是不是还可一与其他的窗口进行交互,把dialog分为模态和非模态的


模态对话框:
    在没有关闭它之前不能与同意应用程序中的其他窗口进行交互。
方法1:
    QDialog dialog(this);
    dialog.exec();
方法2:
    QDialog *dialog = new QDialog(this);
    dialog->setModal(true);
    dialog->show();
    
非模态对话框:
    既可以与他进行交互,又可以与同一应用程序中其他的对话框进行交互。
    new()创建,show()显示。



不用connect函数链接信号和槽:
    源: // connect是QObject中的函数,所以可以直接使用
    connect(ShowChildBtn,SIGNAL(clicked()),this,SLOT(ShowChildDialog()));
    
信号和槽的自动关联:
    把槽函数重命名为:
    on_ShowChildDialog_clicked();    即可。  //由字符on和组建的名字还有信号组成。





设置显示汉字:
    //必须的两句:

    #include <QTextCodec>

    //ubuntu 12.04 

    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());

    //windows 设置要显示的汉字编码

  QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));

    // 其他的一些:
    setWindowTitle(tr("标准对话框"));



QStringList 类:
    The QStringList class provides a list of strings
    
QString QStringList::join(const QString &separator)

Joins all the string list's strings into a single string with each element separated by the given separator.

QStringList类转换为QString类:

QString("%1").arg(Filenames.join(", "));//Filenames 为QStringList的对象,包含n个QString.


QString::arg()方法:
QString QString::arg(const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ))
    返回源string中最低的数字位置被string a 替代后的字符串。
    第二个参数为 a所占的最小空格的数量,第三个是对齐方式 正数向右对齐 负数向左对齐。


examples:
     QString i;           // current file's number
     QString total;       // number of files to process
     QString fileName;    // current file's name

     QString status = QString("Processing file %1 of %2: %3")
                     .arg(i).arg(total).arg(fileName);

    arg()相对于sprintf()的优势:他的数字标记的位置是可以改变的,而且同一个数字的标志位可以是多个,这样就不用一对一了,后面的替代的字符串参数可以一对多。比如一句话中有两个或者多个一样的单词,可以把它们全部标记成%i,不用从前到后顺序的一一对应。


QFontDialog::getFont();
展示字体对话框,并且返回一个字体


实例:
bool ok;
 QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
 if (ok) {
     // font is set to the font the user selected
    fontabel->setText(font.key());         //font.key()返回一个字体的文本方式的表示
    fontlabel->setFont(font);
 } else {
     // the user canceled the dialog; font is set to the initial
     // value, in this case Times, 12.
 }

//********************************************************************************
 // 选择颜色 对话框
void Widget::SetColor(){
   
    QColor color = QColorDialog::getColor(Qt::green,this,tr("选择颜色"),QColorDialog::DontUseNativeDialog);
    qDebug()<<"color"<<color<<endl;
    // 如果颜色是有效的
    /***********等效代码*************
      QColorDialog dialog(Qt::red,this);
      dialog.setOption(QColorDialog::ShowAlphaChannel);//显示Alpha选项
      dialog.exec();
      QColor color = dialog.currentColor();
      qDebug()<<"color"<<color<<endl;
    ******************************/
    if(color.isValid()){
        colorLable->setText(color.name());
        colorLable->setPalette(QPalette(color));
        colorLable->setAutoFillBackground(true);
        //hboxBtnlayout->addWidget(questionBtn);;
    }

}

 //选择文件对话框
void Widget::ChooseFile(){
   
    // 其中4个参数的作用是:指定父窗口,设定对话框标题,默认打开路径,文件过滤器
    QString filename = QFileDialog::getOpenFileName(this,tr("文件对话框"),
                                                    "/home/briup",tr("所有文件(*);;图片文件(*png *jpg);;文本文件(*txt);;浏览器文件(*html)"));
    fileLable->setText(filename);
    qDebug()<<"filename"<<filename;

}

//选择多个文件,注意这里用的是QstringList
void Widget::ChooseFiles(){
      // “/home/briup/”该路径是ubuntu下briup用户的家目录路仅,请修改
    QStringList Filenames = QFileDialog::getOpenFileNames(this,tr("文件对话框"),"/home/briup/",\
                                                          tr("所有文件(*);;图片文件(*png *jpg);;文本文件(*txt);;浏览器文件(*html)"));

    if(Filenames.count()){
        filesLabel->setText(QString("%1").arg(Filenames.join(", ")));
    }

    qDebug()<<"filenames"<<Filenames<<endl;

}


//设置字体对话框
void Widget::SetFont(){
    
    bool ok;
    QFont font = QFontDialog::getFont(&ok,QFont(fontLabel->text()),this,tr("选择字体"));
    if(ok){
        //the user selete a font
        //font.key()返回字体的文本方式的表示,替代..
        fontLabel->setText(font.key());
        //设置字体
        fontLabel->setFont(font);
    }else{
        //the user cancel the dialog
        qDebug()<<"cancel"<<endl;
    }

}

// 输入对话框,字符串,整形,double型,item
void Widget::GetInput(){
    // 输入字符串
    bool ok;
    QString string = QInputDialog::getText(this,tr("输入文字"),tr("输入用户名:"),QLineEdit::Normal,\
                                           tr("admin"),&ok);
    if(ok){
        textLabel->setText(string);
        qDebug()<<"text:"<<string<<endl;
    }
    // 输入整形数
    // 父组件, 对话框名字,提示,默认值,上下范围,每次的变化范围
    int value = QInputDialog::getInt(this,tr("输入整数对话框"),tr("请输入-1000到1000之间的数!"),100,-1000,1000,1,&ok);

    if(ok){
        intLabel->setNum(value);
        qDebug("value:%d",value);
    }

    //输入 双精度                                                                                           小数位
    double value2 = QInputDialog::getDouble(this,tr("输入浮点数"),tr("请输入-1000到1000之间的数值"),0.00,-1000,1000,2,&ok);

    if(ok){
        doubleLabel->setText(QString("$%1.00").arg(value2));
        qDebug()<<"double:"<<value2;
    }

    // 获取条目
    QStringList items;
    items<<tr("条目1")<<tr("条目2");

    QString item = QInputDialog::getItem(this,tr("输入条目对话框"),tr("请输入一个条目"),items,0,true,&ok);
    if(ok){
        itemlanel->setText(QString("[%1]").arg(item));
        qDebug()<<"item:"<<item;
    }


}



// 消息对话框 5个消息 qusetion information warning critical about
void Widget::whichMesg(int MesgID){

    switch(MesgID){
    case 0:                         //qusetion
        QMessageBox::question(this,tr("问题对话框"),tr("你了解qt?"),QMessageBox::Yes,QMessageBox::No);
        break;
    case 1:                        //informatioin
        QMessageBox::information(this,tr("消息对话框"),tr("透明,坚硬的心!"),QMessageBox::Ok);
        break;
    case 2:
        QMessageBox::warning(this,tr("警告!"),tr("你确定要这样操作?"),QMessageBox::Yes,QMessageBox::No);
        break;
    case 3:
        QMessageBox::critical(this,tr("严重错误对话框"),tr("发现一个严重的错误,现在要系统关闭所有文件!"),QMessageBox::YesAll);
        break;
    case 4:
        QMessageBox::about(this,tr("关于baiding1123"),tr("兰州理工阿亮同学"));
        break;
    }

}


// 进度对话框
void Widget::on_processBtn_clicked(){
    QProgressDialog dialog(tr("文件操作进度"),tr("取消"),0,50000,this);
    dialog.setWindowTitle(tr("进度对话框"));
    dialog.setWindowModality(Qt::WindowModal);   //设置对话框为模态
    dialog.show();
    for(int i = 0;i<50000;i++){
        dialog.setValue(i);
        QCoreApplication::processEvents();       // 避免界面冻结
        if(dialog.wasCanceled())break;
    }
    dialog.setValue(50000);
}

// 错误信息
void Widget::errorMesg(){
    QErrorMessage *dialog = new QErrorMessage(this);
    dialog->setWindowTitle(tr("错误消息"));
    dialog->showMessage(tr("出错信息:该文件已损坏!"));

}




   

代码上传到资源中了, 下载点击:Qt标准对话 框


程序是在Ubuntu的Qcreator中写的,若在Windows中编译运行,请在main.cpp 中加入修改编码方式:

QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));
避免出现乱码





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值