QT开发笔记

来源:互联网 发布:知乎温酒作品集 编辑:程序博客网 时间:2024/06/07 13:57

1.显示中文

#include <QTextCodec>

main函数中加

 QTextCodec *texcCode;
 texcCode=QTextCodec::codecForName("GB18030");
 QTextCodec::setCodecForCStrings(texcCode);
 QTextCodec::setCodecForTr(texcCode);
 QTextCodec::setCodecForLocale(texcCode);

 

2.获得部件相对屏幕的绝对地址

部件->mapToGlobal(QPoint(0,0));

 

3.隐去窗口边框

构造函数中加Qt::FramelessWindowHint;如:

Dialog::Dialog(QWidget *parent) :
    QDialog(parent,Qt::FramelessWindowHint)

 

4.刷新界面

QCoreApplication::processEvents();

 

5.让窗口居中显示

在main函数w.show后加

w.move ((QApplication::desktop()->width() - w.width())/2,(QApplication::desktop()->height() -w.height())/2);

  全屏(w.showFullScreen();)

6.以16进制显示某数

例如:label.setText(QString::number(c , 16 ));

 

7.以密文显示

例如 :ui->pwdLineEdit->setEchoMode(QLineEdit::Password);

 

8.关于qDebug():

qDebug("%x",n);无法输出16进制,会是错误的数字。

要这样用

qDebug()<<hex<<253<<endl;

 

9.获取事件前后时间差

QDateTime now = QDateTime::currentDateTime();

事件...

QDateTime then = QDateTime::currentDateTime();

int i = now.secsTo(then);

 

10.超时控制

 QTime dieTime = QTime::currentTime().addSecs(time);
 while( QTime::currentTime() < dieTime )
 {

}

10.设置字体

setStyleSheet("color: rgb(255, 0, 255);font-size : 30px;");

 

11

void QT_vDisplay(int iLine, const char *pcFormat, ...)
{
 if(iLine <=0 )
  return;
 char buf[100];
 va_list  pArg;
 va_start(pArg,pcFormat);
 vsprintf(buf,pcFormat,pArg);
 w->ui.listWidget->item(iLine-1)->setText(buf);
 QCoreApplication::processEvents();
}

 

12在程序中调用另一个程序的方法


uchar execProcess(char* pucLoad)
{
 QProcess proc;

 if(pucLoad == NULL)
  return EM_ERRPARAM;
 char cLoadBuf[256];
 memset(cLoadBuf,0x00,sizeof(cLoadBuf));
 if(pucLoad[0] == '/' || pucLoad[0] == '.')
  sprintf(cLoadBuf,"%s",pucLoad);
 else
  sprintf(cLoadBuf,"./%s",pucLoad);

 proc.start(cLoadBuf);

 qDebug("开始启动进程\n");
 // 等待进程启动
 if (!proc.waitForStarted())
 {
  qDebug("启动失败\n");
  return EM_ERROR;
 }
 // 关闭写通道,因为没有向进程写数据,没用到
 proc.closeWriteChannel();


 // 用于保存进程的控制台输出
 QByteArray procOutput;
 // 等待进程结束
 while (false == proc.waitForFinished())
 {
  ;
 }

 qDebug("进程结束\n");
 return EM_SUCCESS;

}

0 0
原创粉丝点击