qt常用的基本属性设置

来源:互联网 发布:云计算与大数据 编辑:程序博客网 时间:2024/06/01 20:58

1. 设置文本内容

1> 设置控件文本

     ui->button->setText("我是文本");

2> 设置窗口标题

this->setwindowtitle("我是标题");


2. 变量类型转换 

1> 将int转换为string类型

      QString str= QString::number(n, 10);//将10进制整数转换为字符串

2> 将数字转换为string类型

QString str = QString::number(54.3); 


3. 设置大小

1> 设置窗口初始大小

  this->resize( QSize(600,400));

2> 设定窗口大小固定

this->setFixedSize(width,height);

3> 获得窗口大小

int width = this->geometry().width();
    int height = this->geometry().height();

4> 设定控件大小

ui->button->setGeometry(QRect(50,50,100,25));//button在坐标(50,50)的位置,宽100,高50


4. 关闭窗口的命令

1> 关闭主窗口并退出程序是 QApplication::exit(); 

2> 如果是 QDialog,就 accept() 或 reject();

3> 对于所有 QWidget,使用close();


5. 显示对话框

1> 显示模态对话框(窗口未关闭前,不能操作其他)

调用exec()方法

2> 显示非模态对话框

调用show()方法


6. 两个控件的信号相连(按下回车,相当于按下按钮)

connect(ui->lineEdit, SIGNAL(returnPressed()), ui->pushButton, SIGNAL(clicked()), Qt::UniqueConnection);

//Qt::UniqueConnection:防止重复连接。如果当前信号和槽已经连接过了,就不再连接了。


7. 字符串包含某字符串

QString str = " i love you!"

bool has = str.contains("you", QT::CaseSensitive);//QT::CaseSensitive 设置大小写敏感

//包含字符串str, 返回true


8. 设置文本框默认文本(点击输入时消失)

ui->lineEdit->setPlaceholderText("请点击输入");


9.  计算日期间的天数或时间间的秒数

QDate start = ui->dateEdit_start->date();
        QDate end = ui->dateEdit_end->date();
        qDebug() << start.daysTo(end);
//将QDate 换为QTime,  daysTo 换为 secsTo 便计算两个时间的秒数了



0 0
原创粉丝点击