QT-常用的操作

来源:互联网 发布:如何命令提示符测网络 编辑:程序博客网 时间:2024/05/02 07:58

1.QGridLayoutet用法

QGridLayout::addWidget(widget,row,column,rowSpan,columnSpan); 
widget为控件,row,column)为控件占据的左上角单元格位置, 
rowSpan是控件占据的行数,colunmSpan是控件占据的列的个数。
rowSpan和colunmSpan默认值为1。 

2.设置标签字体颜色

 QLabel *msgLabel = new QLabel; msgLabel->setText(tr("<h2><font color = red>此处为消息提示标签</font></h2>"));

3.设置控件与Layout的距离

dowLayout->setMargin(50);

4.设置控件之间的距离

dowLayout->setSpacing(20);

5.创建表格并给值

QStringList fonts;  fonts <<tr("进货单号")<< tr("商品ID") ;  iTableView    = new QTableView(this);                   //显示表格的控件  table         = new QStandardItemModel(v_table.size(), 10);           //创建的表格  iTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);        //不能对表格进行编写  iTableView->setSelectionBehavior(QAbstractItemView::SelectRows);       //只能选择表格的一行  table->setHorizontalHeaderLabels(fonts);                //表格的列名  this->iTableView->setModel(table);  table->setData(table->index(i, 0), tem.goods_i_id);

6.给QListWidget改颜色,点中某列时颜色会发生改变

 QBrush brush1(QColor(150, 181, 218, 255)); brush1.setStyle(Qt::SolidPattern); palette.setBrush(QPalette::Disabled, QPalette::Highlight, brush1); list->setPalette(palette);

7.导入数据到EXCEL表格中

void MainWindow::inputExcel(QString data){    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("file ( *.csv)"));    if(fileName == "")            return;    QTextCodec *code;    code = QTextCodec::codecForName("gb18030");    std::string strCountBuffer = (code->fromUnicode(data)).data();    std::string strbuffer = (code->fromUnicode(fileName)).data();    FILE *fileWrite = fopen( strbuffer.c_str(),"w");    QFile file;    file.open(fileWrite, QIODevice::WriteOnly);    file.write(strCountBuffer.c_str(), qstrlen(strCountBuffer.c_str()));    file.close();}

8.将table中的数据提取出来转化成被*.CVS可以接受的字符流

//此字符流中已经包含了列的头和数据QString getTableData(QStandardItemModel* table){    QString stringData;    int i=0, j=0;    for(int i=0; i<table->columnCount(); i++)    {        stringData+= table->headerData(i, Qt::Horizontal, 0).toString();        stringData+=",";    }    stringData = stringData+"\n";    for(i=0; i<table->rowCount(); i++)    {        for(j=0; j<table->columnCount(); j++)        {            stringData+=table->data(table->index(i, j)).toString()+",";        }       stringData+="\n";    }    return stringData;}

9.改变table间隔颜色

tableView->setAlternatingRowColors(true);tableView->setStyleSheet("alternate-background-color: rgb(170, 255, 255);");tableView->setAlternatingRowColors(true);tableView->setStyleSheet("alternate-background-color: rgb(170, 255, 255);");

10.使某格字体变成红色 

model->item(row, column)->setForeground(QBrush(QColor(255, 0, 0)));  
使某格背景成为红色
table->item(i,j)->setForeground(QBrush(QColor(255, 0, 0)));

11.Qt使用sleep函数

QTime t;t.start();while(t.elapsed()<1000)   QCoreApplication::processEvents();

12.Qt中相对路径和绝对路径问题

(1).资源文件要放在程序运行目录下,而不是程序代码目录下如:test-build-desktop
(2).其格式为  .\\imag\\MacQQ.png只有一个上点。
(3).更改工作目录_chdir("D:\\program\\Qt\\test");

13.设置LineEdit不可编辑

 lineEdit->setEnabled(false);

14.设置QLabel文字居中

 textLabel->setAlignment(Qt::AlignCenter);

15.LineEdit 获得焦点

LineEdit->forcurinEvet;

16.加入底盘标签

trayIcon = new QSystemTrayIcon(QIcon(".\\MacQQ.png"));this->trayIcon->setVisible(true);

0 0
原创粉丝点击