QT 使用的技巧综合总结

1. 子类继承父类的样式表的时候 

#wifiListWidget { border-radius: 20px ;  }

2.label显示的内容太多 需要换行

A. 和label的word Wrap的属性赋值为true

B.代码实现 

//    ui->tempRangeLabel1_3->setWordWrap(true);
//    ui->tempRangeLabel1_3->setAlignment(Qt::AlignTop);

3.很多属性需要在样式表中设置通过代码设置可以这样写

QScrollBar::up-arrow:vertical    //按键上的箭头
QScrollBar::add-line   //为滚动条下面的箭头区域,点一下往下走一行的那个,sub-line则是上面的
QScrollBar:vertical  // 滑动区域


   ui->listWidget->verticalScrollBar()->setStyleSheet("QScrollBar:vertical"
                                                       "{"
                                                       "40px;"
                                                       "background:rgba(14,19,26,50%);"
                                                       "margin:0px,0px,0px,0px;"
                                                       "padding-top:9px;"
                                                       "padding-bottom:9px;"
                                                       "}"
                                                       "QScrollBar::handle:vertical"
                                                       "{"
                                                       "40px;"
                                                       "background:rgba(0,0,0,25%);"
                                                       " border-radius:10px;"
                                                       "min-height:20;"
                                                       "}"
                                                       "QScrollBar::handle:vertical:hover"
                                                       "{"
                                                       "40px;"
                                                       "background:rgba(0,0,0,50%);"
                                                       " border-radius:10px;"
                                                       "min-height:20;"
                                                       "}"
                                                       "QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical"
                                                       "{"
                                                       "background:rgba(0,0,0,10%);"
                                                       "border-radius:10px;"
                                                       "}"
                                                       );

 4.设置控件的伸缩属性

代码实现

    ui->AddPB->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

 5. 读取文件的路径和文件中的文件名字

//    QString  filePath=QFileDialog::getOpenFileName(NULL, tr("File Dialog"),"/home","",0); // tr("pic file(*.txt)")

//    QString filePath = QFileDialog::getOpenFileName(this,"Images (*.txt)",this);
//    QString file_name = QFileDialog::getOpenFileName(this,tr("Open File"),"","",0);

//    qDebug() << filePath << endl;



//    QString  word;
//    QFile file(filePath); // "/home/alvin/Desktop/新建文本文档.txt"
//    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
//        qDebug()<<"Can't open the file!"<<endl;
//    }
//    while(!file.atEnd()) {
//        QByteArray line = file.readLine();
//        QString str(line);
//        word.append(str);
//        qDebug()<< str;
//    }

6.设置input 控件的 字体格式 大小 颜色

//    ui->customAdverInPut->setTextColor(Qt::white);
//    ui->customAdverInPut->setFont(QFont("Timers" , 76 ,  QFont::Bold));
//    ui->customAdverInPut->setText(word);

 7.QCombobox组合框 样式表 举例

QComboBox

{

 250px;

min- 250px;

max- 250px;

height: 100px;

border-radius: 3px;

border: none;

font-size: 12pt;

font-family: 微软雅黑,宋体;

background-repeat: no-repeat;

background-position: center left;

padding-left: 42px;

color: white;

selection-color: black;

selection-background-color: darkgray;

}

QComboBox::drop-down

{

background-color: black;

min- 100px;

}

QComboBox QAbstractItemView

{

border: none;

color: grey;

selection-color: black;

selection-background-color: darkgrey;

}

QComboBox QAbstractItemView::item

{

height: 100px;

min-height:100px;

}

 8.文件读取

//   QString  filePath=QFileDialog::getOpenFileName(NULL, tr("选择文件夹"),"/home/alvin/Pictures",tr("Config Files (*.mp4 *.mp3)"),0);
//   qDebug() << filePath << endl;
//   qDebug() << "截取字符串:" << filePath.section('/',-1,-1) << endl;
//   fileName.append(filePath.section('/',-1,-1));
//   filePathAndName.append(filePath);

//       QString  filePath=QFileDialog::getExistingDirectory(NULL, tr("选择文件夹"),"/home/alvin/Videos",QFileDialog::ShowDirsOnly); //  /home/alvin/Pictures
      
原文地址:https://www.cnblogs.com/wanghuixi/p/8629111.html