vs+qt开发常用代码

1、文本编辑框

/*****************文本编辑框TexiEdit(#include <QTextEdit>)*********************/
 ui->textEdit->setTextColor(Qt::red);  
   // 把显示的文字设置为红色
 ui->textEdit->setColor(QColor(0, 255, 0)); 
 // 设置字体的另外一个方法,后面的值对应着R G B
 ui->textEdit->setText("hello, Qt!");
  // 在文本编辑框设置内容
 ui->textEdit->append("abc");
  // 另起一行追加“abc”
 ui->textEdit->setFontPointSize(20);
  // 设置字体大小,对后面的代码有效,前面的不能改变
 ui->textEdit->insertPlainText("333333");  
 // 开始“333333”
 QString str1 = ui->textEdit->toPlainText();
 // 获得textEdit的内容
 

2、随机时间

/******************随机时间(#include <QTime>)**************************/
 int rand = 0;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
 //以0时0分0秒到现在的秒数为种子, 调用全局的qrand()函数生成随机数
    rand = qrand()%10000;
     //对10000取余,保证位于10000的范围内

3、按钮操作

/*********************按钮操作(#include <QPushButton>)****************************/
 QPushButton *buttonSet; // 对象指针
 buttonSet = new QPushButton("设置", this);
  // 为对象分配空间,并设置内容
    buttonSet->setGeometry(15, 185, 75, 22);
  // 设置button的位置大小
 
 ui->pushButton->setGeometry(QRect(0,0,100,100));
  // 设置button位置大小
 ui->pushButton->setGeometry(0,0,100,100);
  // 设置button位置大小的另一种方法
    ui->pushButton->setText("Ensure");
     // 设置按钮内容
    qDebug()<<"button text = "<<ui->pushButton->text();
     // 获取button里的内容
    ui->pushButton->setIcon(QIcon("../image/on.png"));
     // 设置button里的icon
    ui->pushButton->setIconSize(QSize(50,50));
     // 设置icon的大小
 ui->pushButton->resize(100,50);
  // 重新设置button的大小 
 qDebug()<<__FILE__<<__LINE__;
  // 打印文件名和所在行数
    int pw = ui->pushButton->width();
     // 获取宽和高的两种方式
    int ph = ui->pushButton->height();
    int x = ui->pushButton->geometry().x();
    int y = ui->pushButton->geometry().y();
    ui->pushButton->move(100,100); // 移动button的位置
    ui->pushButton->hide(); // 隐藏button
 ui->pushButton->show(); // 显示button
    ui->pushButton->setEnabled(false); // button不使能
    ui->pushButton->setEnabled(true); // button使能
 
 /**********************按下button的操作***************************/
 QString msg = this->sender()->objectName();
  // 按下button时,获取button的名字,假如名字为“abc”,获取信号发出者的名字
    QPushButton *button; // 定义一个指针对象
    button = (QPushButton *)this->sender();
     // 确定哪个按钮被按下,并接收这个信号的发出者,最好先判断button是否为空,不为空才操作
    msg += "######"+ button->text();
     // 字符串连接,button->text()为获取button里面的内容,如内容为“123” 
    ui->labelDisplay->setText(msg);
     // 在label显示出来,结果为:abc######123
 
 int Num = this->sender()->objectName().at(10).digitValue();
  // 假如名字为toolButton1, at(10)就指向1了,这字符就转化为数字

4、字符串处理

/**********************字符串处理(#include <QString>)*********************************/
 QString str = "123";
    bool OK;
    int m = str.toInt(&OK,16);
     // 字符串转整型, 16表示“123”里面的数字是16进制,10就为10进制,操作成功后,OK返回true
    str = QString::number(m);
     // 整型转换成字符串
    str.append("abc"); // 追加
    str += "hello"; // 追加的另外一种方式
    QString str2;
    str2 = QString("**%1##%2&&%3").arg(123).arg("!!!!").arg(“654”);
     // 组包相当于sprintf,结果为123!!!!654 
 

5、标签操作

/***********************标签操作(#include <QLabel>)***************************************/
 // 操作和pushButton差不多
 labelImage = new QLabel(this);
  // 为label分配一个空间,也可以同时添加名字,操作和pushButton一样
    labelImage->setGeometry(QRect(0, 180, 330, 30));
     // 设置label的位置
    labelImage->setPixmap(QPixmap(":/new/prefix1/QQ_PIC/qq3.jpg"));
     // 在label上设置图片
 ui->labelImage->setScaledContents(true);
  // 图片自动适应label大小
 ui->labelImage->setText("hello Qt world");
  // 设置label里的内容
 QFont font; // 需要头文件(#include <QFont>)
    font.setPointSize(10); // 设置大小
    ui->labelImage->setFont(font); // label设置字体大小
 ui->labelImage->setText(QString("Mike"));
  // 设置label内容的另外一种方式
    QString test = ui->labelImage->text();              // 获得label里面的内容
 ui->labelImage->show(); // 显示label
 this->showFullScreen(); // 全屏显示
 ui->labelImage->resize(20, 20); // 改变label的大小

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值