QT常用函数大全(更新中)

部分转载于百度文库。

显示中文(主要在main函数实现)

/********显示中文(主要在main函数实现) **********/
#include <QTextCodec>编码头文件
QTextCodecx:setCodecForCStrings(QTextCodec::codecForName("gb18030);
//窗口里面可以接收或写中文文字
//这个和上面那个是等级的
QTextCodec:setCodecForLocale(QTextCodec:codecForName("gb18030");
QTextCodec:setCodecForTr(QTextCodecx:codecForName("gb18030);
//窗口部件里(如button)可以中文显示

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  QTextCodex:setCodecForTr(QTextCodex::codecForName("*gb18030));
  QTextCodec:setCodecForCStrings(QTextCodec:codecForName("gb18030));

  QWidget *pWidget = new QWidget;
  QL abel label(pWidget);
  label.setText(QObject:tr("同一个世界,同-个梦想");
  //或label.setText(同一个世界,同一个梦想);
  pWidget- >show();
  return app.exec();
}

QApplicatin:setQuitOnL astWindowClosed (false);
//后台运行,按关闭按钮关闭不了


主窗口操作

/***************主窗口操作******************/
//设置屏幕大小,这里固定为330*210
this- >setMinimumSize (330, 210);
this- >setMaximumSize(330, 210);

this->setWindowlcon(Qlcon(":……" );
//主窗口设置icon, 括号内为icon图片路径

this->setWindowTitle("QQ2012-Qt版本");
//设置窗口的名字

int w = this->width(); //获取其宽度
int h = this ->height); //获取其高度

QWidget *parent = this->parentWidget();
//获得自己的父对象
if (NULL != parent)
{

}//考虑父对象不存在情况

this >setWindowFlag(Qt::FramelessWindowHint);
//无边框

随机时间

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

文本编辑框操作

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

调色板操作

/*********调色板的使用( #include <QPalette>) **********/
QPalette:Window;//通常指窗口部件的背景色
QPatltte::WindowText;//通常指窗口部件的前景色
QPalte:Base;//指文本输入窗口部件的背景色
QPaltte:Text;//指文本输入的窗口部件的前景色
QPaltte::Button;//指按钮窗口部件的背景色
QPaltte:ButtonText;//指按钮窗口的前景色

QPalette p;//定义对象
p.setColor(QPalette :Window, Qt:blue); // Window为大写
p.setColor(QPalette :WindowText, QColor(255, 0, 0));
//颜色设置的另外-种方式
//到第一步,调色板调好了

ui->lcdNumber->setPalette(p); //选择lcdNumber使用这个调试板
ui >lcdNumber- >setAutoFillBackground (true); //自动填充背景色

按钮操作

/**********按钮操作( #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->setlcon("..."));
//设置button里的icon   "..."里为图片地址
ui->pushButton->setlconSize(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了,这字符就转化为数字

/**********设置按钮背景色**************/
setStyleSheet("background-color: rgb(255, 255, 0)");

标签操作

/*************标签操作(#include <QLabel>) *******************/
//操作和pushButton差不多
labelImage = new QLabel(this);
//为label分配一个空间,也可以同时添加名字,操作和pushButton-样
labelImage >setGeometry(QRect(0, 180, 330, 30));
//设置label的位置
labelImage ->setPixmap(QPixmap(":/new/prefix1/QQ PIC/q.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); // labe股置字体大小
ui->labelImage >setText(QString("Mike");
//设置label内容的另外一种方式
QString test = ui->labelImage->text(); // 获得label里面的内容
ui->labelImage >show(); //显示label
this ->showFullScreen(); //全屏显示:
ui->labelImage >resize(20, 20); //改变label的大小

/****** (#include <QComboBox>) *******/
comboBoxAccount = new QComboBox(this);
//为combobox分配空间
comboBoxAccount ->setGeometry(95, 90, 115, 20);
//设置位置
comboBoxAccount ->setEditable(true);
//设置状态为可编辑的
comboBoxAccount->insertltem(0, "1234567");
//在第0行(通常所说的第-行)插入内容
comboBoxAccount->insertem(1, "231435353");
//在第1行(通常所说的第二行)插入内容

ui->comboBox- >setCurrentIndex(0);
//显示第0行的内容
QString currText = ui->comboBox->currentText();
//获取combobox当前的内容
int index = ui->comboBox->currentIndex();
//换取当前的索引号

字符串处理

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

行编辑

/********************行编辑( #include <QLineEdit>) *******************/
lineEditPassword = new QLineEdit(this); //分配空间
lineEditPassword->setGeometry(95, 115, 115, 20); //设置位置
lineEditPassword->setEchoMode(QL ineEdi::Password);
//状态设置为密码模式

ui->lineEdit->setText("aaa"); //设置内容
ui->lineEdit->setText("bbbbb"); //插入内容
QString str = ui->lineEdit->text(); //获取内容
/********************checkbox( #include <QCheckBox>) *******************/
checkBoxRemQQ = new QCheckBox("记住密码", this); //分配空间
checkBoxRemQQ->setGeometry(95, 150, 70, 20); //设置位置

if(ui->checkBoxRemQQ->isChecked()) //判断是否按下
  qDebug()<<"checkBoxRemQQ is Checked" ;

动画操作

/************动画操作( #tinclude <QMovie>) ***************/
movie = new QMovie;
movie-> setFileName(":/image/boy.gif");
//设置gi动画,.... 为图片路径
//或者QMovie *movie = new QMvt*(":/boom.gif");
ui->label->setScaledContents(true); //自适应大小
ui->label->setMovie(movie); //在label设置动画
movie >start(); //开启动画

数码管操作

/********数码管操作( #include <QLCDNumber> ) ************/
ui->lcdNumber- >setDigitCount(5); //设置数码管显示5个数字
ui->lcdNumber->setNumDigits(5); //等价 上面
ui->lcdNumber->display("ABC"); //让数码管显示内容

进度条操作

/***进度条操作( #tinclude <QProgressBar>) ******/
ui->progressBar->setMinimum(0); //设置进度条的最小值
ui->progressBar->setMaximum(200); //设置进度条的最大值
ui->progressBar- >setValue(99); //在进度条所占的比例,并显示出来

布局操作

/**********布局操作 **************/
QPushButton *button;
this->button.setParent(this); /用this不易出错,指定父对象,
this->button.setText("click me!"); //设置button内容
ui->horizontalL ayout->addWidget(&button);
//将部件加到水平布局管理器
ui-sverticalLavout>addWidqet( &button);
ui->horizontalLayout- >addWidget(&button);
//将部件加到水平布局管理器
ui->vericalLayout->addWidget(&button);
//将部件加到垂直布局管理器
ui->gridLayout- >addWidget(&button,1,1,1,1);
//将部件加到网格布局管理器

定时器操作(结合lcd 与 动画操作,倒计时结束播放动画)

/********定时器操作( #include <timer> ) ************/
/*******在构造函数里定义*******/
QTimer *timer = new QTimer();
//括号里加不加this都可以
//定时器start之后每隔1s发送一个 timeout()信号,每隔1S会执行一次mySlot)槽函数
connecttimer, SIGNAL(timeout()), this, SLOT (mySlot) ));
timer->start(1000);
//以ms为单位,启动定时器,此次即为1秒
/**************槽函数mySlot()的处理****************/
static count= 11;// 静态变量,count的值每次改变都记录下来
count--; // 10,9,8,7,8……倒计时
ui->lcdNumber- >display(count); //在lcd上显示
if (0 == count)
{
  timer->stop0); //关闭定时器
  ui->lcdNumber- >hide(); //隐藏LCD
  ui->label->show(); //显示label
  this->showFullScreen();//全屏显示
  ui->label->movie()->start();// 动画启动
}
/******信号和槽的使用(两个u切换)*******/
//两个ui分别为mainWidgt, secondaryWidgt, 其中mainWidgt为主界面, 界面对应类的名字和u名字相同
//secondaryWidgt类中声明信号
signals:
void goBack();
//在mainWidgt类,定义一个secondaryWidgt的指针对象*w, 接着在其构造函数操作
this->W = new secondaryWidgt; //分配空间
//信号和槽连接,当接收到secondaryWidgt发出goBack()信号,就显示mainWidgt窗口
this- >connect(w. SIGNAL(goBack(), this, SLOT(showl);
//mainWidgt窗口按下按钮时,隐藏自己(this->hide(), 显示secondaryWidgt窗口(W->show())
//secondaryWidgt窗口按下按钮时,隐藏自己(this->hide()).发送goBack()信号(emit this->goBack())

文件夹操作

/***********文件夹操作(include <QDit>)*********/
QDir tempDir; //文件夹变量
QString str = QCoreApplication :applicationDirPath); //获取当前应用程序路径(#include <QCoreApplication>)
str += "/outputlmage";
if (false == tempDir.exists(st)) //当这个文件夹不存在时,才创建
{
  bool ok = tempDir.mkdir(str);
  //循环创建,直到成功创建
  while (false == ok)
  {
    ok = tempDir.mkdir(str);
  }
}

文件操作

/******文件操作1( #tinclude <QFileDialog>) ************/
QString str;
str = QFilDialog::getOpenFileName(this, "MyOpenile","../",
   "debug (*.0 *.cpp);txFile(*.txt);All File(".")" );
//"MyOpenFile为打开新窗口的名字,"../"为文件打开的上级路径
//"debug (*.0 *.cpp);txFile(*.txt);All File(".")"为文件打开的格式
//str接收的是打开文件的路径

/************文件操作2(#include <QFile>)****************/
//写操作

QFile file("1.txt"); //创建文件对象
//一般不要IO_ ReadWrite,很容易出现赃数据
  //如果要在文件的后面添加内容,要IO_ WriteOnly/IO_ Append
  //如果要清空原来的内容,只要IO_WriteOnly
if(file.open(QIODevice:WriteOnly)){ //只写方式打开文件
  QTextStream Out(&ile);
  out.setCodec(QTextCodec:codecForName("gb18030")://写之前设置编码
  out<<i<<"
"<<"image"<<"
"<< Label[i]->x() <<"
"<<Label[i]->y()<<"
"<<Label->[i]width()<<"
"<<Label->[i]height()<<"
"<< *this- >picPath[i]
   << "$$"<< this->cardBackgound<< "/n";
file.close();
}

//读操作
QFile flie(fileTempPath);
QString dataLine;
//只读方式打开
if (file .open(QlODevice :ReadOnly){
  QTextStream readData(&file);
  readData. setCodec(QTextCdec:codecForName("gb180")); //读之前也设置编码
  while (false == readData.atEnd()) {// 是否是到文件的结尾
    dataLine = readData.readLine(); //先读一行
    if (dataLine.size() != 1){
       this->flag = dataLine. section(" ",0,0).tolnt())://坐标下标
       this >data[flag][O]=dataLine.section(" ",2,2).tolnt()); //x 
       this->data[flag][1] = datal ine. section(" ",3,3),.tolnt())//y
       this >data[flag][2]=dataLine section(" ",4,4).tolnt()); //w
       this >data[flag][3] = dataLine section("  ",5,5).tolnt()); //h
    }
  }
  file. close();
}

颜色操作

/********颜色设定( tinclude <QColorDialog> ) ************/
QColor mycolor;
mycolor = QColoDialog::getColor); //获取颜色
if ( false == corlor.isValid)
//判断颜色是否有效,不加这个判断,颜色变回默认值
return;
QPalette palette;//画笔
palette. setColor(QPalette :Base, mycolor); //设置颜色

//设置颜色的另外一种方式
QBrush brush(mycolor); //使用颜色
palette. setBrush(QPlete:Active, QPetltte::Base, brush); //使用笔刷
ui->colerLineEdit->setPalette(palette); //用画笔画

*****常用对话框( #include <QMessageBox) *..................****
询问
int button = QMessageBox::question(this, "MyQuestion", "Are you OK?",
QMessageBox:No I QMessageBox:Yes| QMessageBox:Cancel);
switch(button){
case QMessageBox::No:
ui->label->setText("NO");
break;
case QMessageBox::Yes:
ui->label->setText("Yes");
break;
case QMessageBox:Cancel:
ui->label->setText("Cancel");
@申I
break;
********信息对话框************
QMessageBox:information(this, "my information", "Game over");
QMessageBox:information(this, "my information", "Game over");
********警告对话框********
QMessageBox:warning(this, "my Warning", "Your system have a serious problem !nlt Will turn off in 3 min." );
“常用事件处理

字体设置

/****字体设置( #include <QFontDialog>) *************/
bool ok = false;
QFont font = QFontDialog::getFont(&ok); //得到字体
if(true== ok) //如果字体有效,防止字体恢复成默认值
ui->fontL ineEdit: >setFont(font);//设置字体

常用对话框操作

/*****常用对话框( #include <QMessageBox) *****/
/*******询问**********/
int button = QMessageBox::question(this, "MyQuestion", "Are you OK?",
QMessageBox:No I QMessageBox:Yes| QMessageBox:Cancel);
switch(button){
  case QMessageBox::No:
    ui->label->setText("NO");
    break;
  case QMessageBox::Yes:
    ui->label->setText("Yes");
    break;
  case QMessageBox:Cancel:
    ui->label->setText("Cancel");
    break;
}
/********信息对话框************/
QMessageBox:information(this, "my information", "Game over");
QMessageBox:information(this, "my information", "Game over");
/********警告对话框********/
QMessageBox:warning(this, "my Warning", "Your system have a serious problem !\nlt Will turn off in 3 min." );

常用事件处理

/***********常用事件处理*********/
/*****标点事件( #include <QMouseEvent>) ******************/
void mouseMoveEvent ( QMouseEvent *e);
void mousePressEvent ( QMouseEvent *e);
void mouseReleaseEvent ( QMouseEvent *e);
void mouseDoubleClickEvent( QMouseEvent *e);

//鼠标移动事件,默认是按下移动才启动事件
void Widget:mouseMoveEvent(QMouseEvent *e)
//事件函数名字必须这样,不能改变,因为这个是虚函数
ui->label->setTex("(" +Qting:numble(e->())+","+QString:number(e->y()+")");
//显示其坐标
//要想不需要按下移动,也能启动事件,在构造函数里加下面的函数
this- >setMouseTracking(true);


//鼠标点击事件
void Widget:mousePressEvent(QMouseEvent °e)
QString s="";
if(e->button()-=Qt:LeftButton) //左击
  s ="LeftButton Pressedn";
if(e->button()= Qt::RightButton) //右击
  s ="RightButton Pressedn";
if(e->button()= Qt::MidButton) //中间滑轮点击
  s = "MidButton Pressedn";
//鼠标释放事件,操作和点击一样
void Widget:mouseReleaseEvent(QMouseEvent *e)
//滑轮滚动事件
void Widget:wheelEvent(QWheelEvent °e)
QString s;
if(e->orientation()== Qt:Vertical)
{
//判断滚轮是否垂直滚动
  if(e->delta()>0) //大于0为滚轮向上
    s +=" go (head)";
  else //小于0 即为向下
    s +=" go (back)";
}

/****************键盘事件( #include <QKeyEvent>) ************/
//修饰键操作
void MykeyEvent::keyPressEvent(QKeyEvent *e)
QStrings s="";
switch(e ->modifiers()){
//修饰键选择,可以达到 Ctrl+A这样的效果
case Qt:ControlModifier: // Ctr键
  s = tr("Ctrl+");
  break;
case Qt:AltModifier: // Al键
  s = tr("Alt+");
  break;
}
switch(e->key()) //普通按键
{
case Qt:Key_ Left: //方向键的向左
  s += tr("Left Key Press");
  break;
case Qt:Key_ Right: //方向键的向右
  s += tr("Rigth_ Key Press");
  break;
case Qt:Key_ Up: //方向键的向上
  s += tr("Up_ Key Press");
  break;
case Qt::Key_ Down: //方向键的向下
  s += tr("Down. Key Press");
  break;
case Qt::Key_ Z://Z键
  s +=tr("Z_ Key Press");
  break;
}
ui->label- >setText(tr(s.toAscii()));
//转化为ASCI码,再显示

//主窗口大小发生变化时被调用
void MykeyEvent:resizeEvent(QResizeEvent *e)
qDebug()<<"new size = "<<e ->size(); /变化后的窗口大小
qDebug()<<"old size = "<<e ->oldSize(); //变化前的窗口大小

绘图事件

/****************绘图事件( #include <QPaintEvent>) ***************/
//利用QPainter绘制各种图形,在Void paintEvent(QPaintEvent *event)中实现
//当窗口被绘制时被调用,也可以通过update()产生paintEvent(……)事件
//绘制的内容以背 景的形式出现在窗口中,QPainter一般要放在 paintEvent()里,否则会初始化失败

event- >rect();// 可得到需要重新绘制的区域
QPainter painter(this);//创建对象
painter.setPen0;//设置画笔
painter.setBrush() //设置画刷
patiner.drawX();//画
drawPoint()//画点
drawLine()//画直线
drawRect()//画矩形
drawEllipse()//画椭圆
drawPicture()//画图片
drawlmage()//绘图片
drawPixmap()//绘图片
drawText()//绘文本


//绘图事件
void PaintEvent:paintEvent(QPaintEvent * event)
qDebug()<< event->rect); /运行结果为: QRect(0,0 400x300)
QPainter p(this); /创建画笔对象,需要头文件#include <QPainter>
QPen pen; // tinclude <QPen>,创建一支画笔, 下面是配置画笔
pens.setColor(Qt:red); //画笔的颜色
pen.setStyl(Qt:DashDotDotLine);//画笔画线的类型,虚线
pen. setWidth(3); //画笔画线的宽度
p.setPen(pen); //把画笔交给画家画
p.drawLine(10,10,260,10); //以(10, 10)为起点,以(260, 10)为终点,画直线
p. drawRect(10,20,200,150); //以(10, 20)为起点,宽为200,高为150,画矩形

QBrush brush; //创建画刷,在上面刷东西,需要头文件#include <QBrush>
brush. setColor(Qt:magenta); /设置颜色,粉红色
brush.setStyle(Qt:DiagCrossPatterm); //画刷的风格,网格风格
p.setBrush(brush); //装画刷交给画家
p.drawRect(10,20,200,150); //以(10, 20)为起点,宽为200,高为150的矩形里面刷东西
p setPen(QPen()); //画笔恢复默认值
p.setBrush(QBrush()); //画刷恢复默认值
p.drawellipse(230,30,150,200); //以(230, 30)为起点,宽为150, 高为200的矩形画内切圆
p.setBrush(QBush(Qixma./imag.tstj))); //画刷里面的内容是图片
p.drawPixmap(O, 0, this >width), this->height(), QPixmap(":/image/on.png")); //以窗口大小画图
//画图事件调用update()会使整个窗口的图片刷新, 不要再绘图事件调用update()、播放动画、设置button的图片


//设置字体
QFont font; //需要头文件#include <QFont>
font.setFamily("幼圆"); //设置字体类型
font.setPointSize(30);设置字体大小
font.setBold(true); //是否加粗
font.setUnderline(rue); //是否加下划线
font.setStrikeOut(rue); //设置横穿文字中间的线
p.setFont(font); //把字体交给画家
p.setPen(Qt:red);//设置画笔颜色
p.drawText(20, 190,"你好,Qt! ");//在(20, 190)为起点,写字

//这和绘图事件无关,截图
QPixmap image = QPixmap:grabWidget(this, 0, 0, this ->width(), this >height());
image save("1.png"); / 保存图片

播放音乐

/****************播放音乐(#include<QSound>) ***************/
//一种方法
QSound::play("mysounds/bells.wav" );
//另一种方法
QSound bll("mysounds/bells.wav" );
bells .play();
//也就是说
QSound *bells = new QSound("mysounds/bells.wav");
bells->play();
bells- >setLoops(-1); //无限循环

使用枚举

/****************攻举的使用**********/
//首先在一个类中定义一个枚举,如下
class A
public:
enum B{Empty, Black, White};
//在类中的成员函数了,可以直接操作,如:
BC= Empty;
//假如在别的类中,创建类A的对象指针a,如a = new A(this);
//这里有两种使用方法
A::B test = a->Black;
A::B test = A::Black;

宏使用

//宏定义
#define print qDebug()<<_ FILE_ <<_ LINE_ < <":"

memcpy的使用

/*************memcpy的使用****************/
//函数的原型: void "memcpy(void 'dest, const void 'src, size_ t count);
int diretincpy[8][2] = {{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,-1},{-1,0}};
int direction[8][2];
//函数的功能是将directioncpy的内容拷贝到direction
// sizef(int)*16,注意这个长度
memcpy(direction,directioncpy, sizeof(int)*16);

int chessHistroy[64][8][8];
int chess[8][8];
memcpy(chessHistroy[histroy],chess,sizeof(int)*64);

重写鼠标事件

/*************重写鼠标事件****************/
QPoint dragPosition; //在类中增加这 一个成员

void Widget::mouseMoveEvent(QMouseEvent *e){
 if(e->buttons() & Qt:L etButton) { //当满足鼠标左键被点击时
   move(e->globalPos()-this >dragosition); //移动窗口
}

void Widget:mousePressEvent(QMouseEvent *e){
if(e->button) == t:LettButton){//点击左边鼠标
  //globalPos();获取根窗口的相对路径,frameGeometry().topLeft()获取主窗口左上角的位置,
  //frameGeometry0.topl ef(相当于起点坐标: this->x0, this->y0;
  this->dragPositione >globaPos()-this ->frameGeomety()topLet(): //和下面操作等价的
  // this->dragPosition.sel(e->x());
  // this->dragPositionsetY(e->y());

  //qDebug() << this ->frameGeometry().topLeft(); /等价下面
  //qDebug() << this->x0 <<""<< this->y();

  //qDebug() << e->globalPos0; //等价下面
    //qDebug() << e->X0+x0)<<""<< e->y()+y();
}
else if(e->button() == Qt:RightButton) {
  this >close();
}

QStringList输出

/*******QStringList*************/
QStringList list"hello");
qDebug()<<list; // ("hello")

//追加
list.append("word"):
qDebug()<<list;//("hello", "word")

//list<<"qt"<<"listen";实际是加了这一步的
qDebug()<<list;//("hello", "word", "qt", "listen")

//合并
QString str;
str = list.join(",");
qDebug()<<str; // ("hello,word, qt,listen")

//拆分
str = "hello,word,qt";
QStringList list1;
list1 = str.split(",");
qDebug()<<list1; // (hello", "word', "qt")

//去掉空格
list1 = strsplit(",",QString::SkipEmptyParts);
qDebug()<<list1;// ("hello", "word", "qt")

//索引
int num; 
num = str .indexOf(QRegExp("o"));
qDebug()<<"num = "<<num; // num= 4

num = list1.indexOf(QRegExp("hello"));
qDebug()<<"QStringList list1 = "<<num; // QStringList list1 = 0

num = str.lastIndexOf(QRegExp("o"));
qDebug()<<"last num = "<<num; // last num= 7

num = list1 .lastIndexOf(QRegExp("hello"));
qDebug()<<"last QStringList = "<<num; //last QStringList= 0
//替换("hello", "word", "qt", "listen")
list.replacelnsStings("o","eee");
qDebug()<<"replace list= "<<list;
// (helleee, "weeerd", "qt", "listen")

list.replace(2,"replace");
qDebug()<<"replace only one list="<<list;
// (helleee, "weeerd", "replace", "listen")

//过滤("hello", "word", "qt)
QStringList.result ;
result = list1.filter("o");
qDebug()<<*filter result = "<<result; // ("hello", "word")

//查找
if(listt .contains("hello")) // true
  qDebug()<<"true"

撰写不易,留个赞呗

  • 26
    点赞
  • 134
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Qt Creator是一个跨平台的C++集成开发环境(IDE),专门用于开发Qt应用程序。它提供了一整套工具,包括代码编辑器、可视化GUI设计工具、调试器、静态代码分析器和自动构建工具等,可以帮助开发者更加高效地开发Qt程序。以下是Qt Creator的一些特点和使用方法: 1.特点: - 支持多种编译器和调试器,包括GCC、Clang、MSVC和MinGW等。 - 提供了丰富的代码编辑功能,包括语法高亮、自动补全、代码折叠、代码重构等。 - 集成了可视化GUI设计器,可以通过拖拽和放置的方式快速创建用户界面。 - 支持多种版本控制系统,包括Git、Subversion和Perforce等。 - 提供了强大的调试功能,包括断点调试、变量监视、堆栈跟踪等。 - 支持自动化构建和部署,可以快速生成可执行文件和安装包。 2.使用方法: - 安装Qt Creator:可以从Qt官网下载Qt Creator的安装包,也可以通过包管理器安装。 - 创建项目:在Qt Creator,可以通过“File”->“New File or Project”来创建新项目。选择“Qt Widgets Application”或“Qt Quick Application”模板,然后按照向导进行设置即可。 - 编写代码:在Qt Creator,可以通过“Projects”视图来管理项目文件和设置构建选项。在“Edit”视图,可以编辑代码并使用各种代码编辑功能。 - 设计用户界面:在Qt Creator,可以通过“Design”视图来设计用户界面。可以使用拖拽和放置的方式添加控件,并设置其属性和布局。 - 调试程序:在Qt Creator,可以通过“Debug”视图来调试程序。可以设置断点、监视变量、查看堆栈等。 - 构建和部署:在Qt Creator,可以通过“Build”视图来构建程序。可以选择不同的构建配置和目标平台,并生成可执行文件和安装包。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值