持续记录 Qt开发

https://doc.qt.io/archives/qt-4.8/stylesheet-examples.html

创建带单位的QLineEdit

  VariableConfig->setStyleSheet(QString::fromUtf8("QWidget#widget_name{\n"
"    border-radius: 6px;\n"
"       border: 1px solid rgb(130, 130, 130);\n"
"       background-color: rgb(255, 255, 255);\n"
"}\n"
"QLineEdit#le_varName{\n"
"       border: none;\n"
"       background-color:transparent;\n"
"}\n"
"QLabel#lbl_varNamePrefix{\n"
"       color: rgb(179, 179, 179);\n"
"}\n"
""));

内置窗口风格

#include <QStyleFactory>
//打印支持的所有风格
QStyleFactory::keys();
QApplication::setStyle(QStyleFactory::create("plastique"));

如何删除widget里的布局

void JQPanel_Steel::removeLayout(QWidget *wdialog)
    int itemCount = wdialog->layout()->count(); // layout是你前一个布局
    for (int i = (itemCount - 1); i >= 0; --i) //从末尾开始是因为你删除会影响布局的顺序。例如你删掉第一个,后面的会往前移,第二就变成第一个,然后这时你要是++i的话,就是删掉原来布局里的第三个,这第二个被跳过了。
        QLayoutItem *item = wdialog->layout()->takeAt(i);
        if (item != 0)
            wdialog->layout()->removeWidget(item->widget());
            delete item->widget(); //(ps:如果是子控件不是QWidget,这里会出错,要注意)
    delete wdialog->layout();

一. QTreeWidget

  • QTreeWidget 设置自动自适应字体调整宽度或者强制宽度,设置换行自动设置对比颜色代码如下:
  •  m_SteelTree->setAlternatingRowColors(true); //设置自动换行颜色
     m_SteelTree->header()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应宽度
    //如果只有2列,设置强制宽度代码如下
     m_SteelTree->setColumnWidth(0,  320);
     m_SteelTree->setColumnWidth(1,  100);
    
  • QTreeWidget 如何获取 某一行某一列 被单击/双击:
  • QTreeWidgetItem *colorItemChild = new QTreeWidgetItem(); // ..... connect(m_SteelTree, &QTreeWidget::itemDoubleClicked, this, [=](QTreeWidgetItem *ptr,int col){ if (ptr == colorItemChild) if (col == 1) { //点击的是第一行 QString r, g, b;
  • QTreeWidget 如何设置编辑属性【如可点击,可选中, 可编辑】
  •  QTreeWidgetItem *ptr = new QTreeWidgetItem;
     if (col == 1)
         ptr->setFlags(ptr->flags()|Qt::ItemIsEditable); //可编辑
        ptr->setFlags(ptr->flags()&~(Qt::ItemIsEditable)); //不可编辑
     ptr->setFlags(ptr->flags()&~(Qt::ItemIsEnabled)); //视图中会灰掉且不可编辑
    
  • QTreeWidget如何设置表头不可见
  • QTreeWidget *m_pNewTree=new QTreeWidget();
    m_pNewTree->header()->setVisible(false); //隐藏表头
    

    设置代理模式,鼠标点击QTreeWidgetItem, 变为QLineEdit后,若用户在输入状态,此时如果要鼠标移开窗口,自动变为编辑完状态,该怎么办呢?
    问题如下图: [鼠标移动到 1,自动确认编辑完成]
    调用上面的static 函数,获取支持的 样式list,我的是 ("windowsvista", "Windows", "Fusion")
    然后直接调用 m_tree->setStyle(QStyleFactory::create("windowsvista"));就OK了
    样式图如下:

    1.如果我们写好了下面的信号槽:
    connect(m_combo, &QComboBox::currentTextChanged, this, ....); 2. 调用clear(),clear会触发TextChanged(), 貌似是删到最后一个的时候会造成数组越界; 解决方法如下:
    我们在调用clear()之前,先disconnect() -> clear() -> connect()
    #define FILE_PATH "/tmp/ui_report.txt" //1. write QFile mapDat("/Users/myname/projectname/file.dat"); if (!mapDat.exists()){ qDebug() << "not exist"; QString errMsg; QFileDevice::FileError err = QFileDevice::NoError; if (!mapDat.open(QIODevice::ReadOnly) | QFile::Text){ errMsg = mapDat.errorString(); err = mapDat.error(); qDebug() << "could not open it" << err << errMsg; return; QTextStream in(&mapDat); QString mText = in.readAll(); qDebug() << mText; mapDat.close(); void writeToFile(const QString line) QFile file(FILE_PATH); if (!file.open(QIODevice::WriteOnly | QIODevice::Append) ) return; QTextStream stream(&file); stream << line << endl; f.close(); //2.read void readFile(QStringList &outputStr) QFile f(FILE_PATH); if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&f); while (!in.atEnd()) QString line = in.readLine(); outputStr << line; f.close();

    5. qss样式表

    QPushButton 样式

    const QString pStyled =
            "QPushButton{ \
            font-size: 18px;\
            font-family: Microsoft YaHei;\
            border: 0px solid;\
    background-color:#E1FFFF;\
    color:#000000;\
    border-radius: 20px;\
    QPushButton:disabled{\
        background-color:#DCDCDC;\
        border-radius:20px;\
    color:#FFFFFF;    \
    QPushButton:hover{\
    border: 0px solid;\
        background-color:#f67469;\
        border-radius:20px;\
    color:#000000;\
    opacity:0.2;\
    QPushButton:pressed{\
        background-color:#c44237;\
        border-radius:20px;\
    color:#FFFFFF;    \
    

    6. 分隔splitter

     QSplitter *splitter = new QSplitter(this);
    /* 设置方向 */
     splitter->setOrientation(Qt::Horizontal);
    // 添加widget,只有addwidget函数,不能addLayout
    // 如果有多个控件要add,可以把多个控件放在一个widget里布局好,
    //再用spliiter add 
    splitter->addWidget( widget1 );
    splitter->addWidget( widget2 );
     /*分隔比例, 注意一定要在add完widget才能写,写在addwidget前无效*/
     splitter->setStretchFactor(0,3);
     splitter->setStretchFactor(1,7);
    

    7.QMessageBox

    我一般都这么写

    QMessageBox::information(NULL, "通知", msg,QMessageBox::Yes);                            
    QMessageBox::warning(NULL, "警告", msg, QMessageBox::Yes);  
    QMessageBox::question(NULL, "问题", msg, QMessageBox::Yes);
    QMessageBox::critical(NULL, "严重", msg, QMessageBox::Yes);
    

    8. 一个界面类的css样式设置

    比如我画了个widget,或者代码写了个widget,我要设置界面中2个button的样式
    画的话: ui.btn_view
    手写: m_btn_view

    this->setStyleSheet(R"(
                            QPushButton#btn_view,#btn_recently
                            background-color:transparent; /*背景色*/
                            border-style: outset;
                            border-width: 1px;
                            border-radius:5px; /*边界圆滑*/
                            border-color: #DCDCDC;
                            font: bold 15px;
                            color: black; /*字体颜色*/
                            font-family:Microsoft YaHei;
                            padding: 5px;
                            QPushButton#btn_view:hover,#btn_recently:hover
                            background-color: #C0C0C0;
    

    9. Qt内置图标

    QGridLayout *layout = qobject_cast<QGridLayout *>(ui->centralwidget->layout());
        int IconIndex = 0;
        for (int row = 0; row < 10; row++){
            for (int col = 0; col < 10; col++){
                enum QStyle::StandardPixmap Icon = (enum QStyle::StandardPixmap)IconIndex;
                layout->addWidget(new QPushButton(QApplication::style()->standardIcon(Icon),
                                                  QString::number(IconIndex)), row, col);
                IconIndex++;
     QStyle* style = QApplication::style();
        QIcon openPicture = style->standardIcon(QStyle::SP_DialogYesButton);
        QPushButton *btn = new QPushButton("JQBIM");
        btn->setEnabled(false);
        btn->setIcon(openPicture);
    # 用法2
    ui->pushButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_ArrowRight));
    

    QWebEngine 依赖的谷歌

      // Event handlers
        bool event(QEvent *event) override;
        virtual void mousePressEvent(QMouseEvent *event);
        virtual void mouseReleaseEvent(QMouseEvent *event);
        virtual void mouseDoubleClickEvent(QMouseEvent *event);
        virtual void mouseMoveEvent(QMouseEvent *event);
    #if QT_CONFIG(wheelevent)
        virtual void wheelEvent(QWheelEvent *event);
    #endif
        virtual void keyPressEvent(QKeyEvent *event);
        virtual void keyReleaseEvent(QKeyEvent *event);
        virtual void focusInEvent(QFocusEvent *event);
        virtual void focusOutEvent(QFocusEvent *event);
        virtual void enterEvent(QEvent *event);
        virtual void leaveEvent(QEvent *event);
        virtual void paintEvent(QPaintEvent *event);
        virtual void moveEvent(QMoveEvent *event);
        virtual void resizeEvent(QResizeEvent *event);
        virtual void closeEvent(QCloseEvent *event);
    #ifndef QT_NO_CONTEXTMENU
        virtual void contextMenuEvent(QContextMenuEvent *event);
    #endif
    #if QT_CONFIG(tabletevent)
        virtual void tabletEvent(QTabletEvent *event);
    #endif
    #ifndef QT_NO_ACTION
        virtual void actionEvent(QActionEvent *event);
    

    如何使鼠标变成转动的圆圈让用户等待

     QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
     QThread::sleep(5);
     QGuiApplication::restoreOverrideCursor();
    

    qt 坐标

    创建带单位或者提示的QLineEdit

     auto widget_name = new QWidget;
        widget_name->setObjectName(QString::fromUtf8("widget_name"));
        widget_name->setMinimumSize(QSize(200, 35));
        widget_name->setMaximumSize(QSize(200, 35));
        auto horizontalLayout_6 = new QHBoxLayout(widget_name);
        horizontalLayout_6->setSpacing(0);
        horizontalLayout_6->setObjectName(QString::fromUtf8("horizontalLayout_6"));
        horizontalLayout_6->setContentsMargins(0, 0, 0, 0);
        auto lbl_varNamePrefix = new QLabel(widget_name);
        lbl_varNamePrefix->setObjectName(QString::fromUtf8("lbl_varNamePrefix"));
        lbl_varNamePrefix->setMinimumSize(QSize(40, 35));
        lbl_varNamePrefix->setMaximumSize(QSize(40, 35));
        lbl_varNamePrefix->setStyleSheet(QString::fromUtf8(""));
        lbl_varNamePrefix->setText(QString::fromUtf8("V_B_"));
        lbl_varNamePrefix->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
        horizontalLayout_6->addWidget(lbl_varNamePrefix);
        auto le_varName = new QLineEdit(widget_name);
        le_varName->setObjectName(QString::fromUtf8("le_varName"));
        le_varName->setMinimumSize(QSize(0, 33));
        le_varName->setMaximumSize(QSize(16777215, 33));
        le_varName->setText(QString::fromUtf8(""));
        horizontalLayout_6->addWidget(le_varName);
        widget_name->setStyleSheet(QString::fromUtf8("QWidget#widget_name{\n"
                                                     "    border-radius: 6px;\n"
                                                     "       border: 1px solid rgb(130, 130, 130);\n"
                                                     "       background-color: rgb(255, 255, 255);\n"
                                                     "}\n"
                                                     "QLineEdit#le_varName{\n"
                                                     "       border: none;\n"
                                                     "       background-color:transparent;\n"
                                                     "}\n"