美文网首页
持续记录 Qt开发

持续记录 Qt开发

作者: 诸事圆成 | 来源:发表于2021-03-24 09:25 被阅读0次

    如何删除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

    1. QTreeWidget 设置自动自适应字体调整宽度或者强制宽度,设置换行自动设置对比颜色代码如下:
     m_SteelTree->setAlternatingRowColors(true); //设置自动换行颜色
     m_SteelTree->header()->setSectionResizeMode(QHeaderView::ResizeToContents);//自适应宽度
    
    //如果只有2列,设置强制宽度代码如下
     m_SteelTree->setColumnWidth(0,  320);
     m_SteelTree->setColumnWidth(1,  100);
    
    1. 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;
                   //处理
              }
          }
      });
    
    1. QTreeWidget 如何设置编辑属性【如可点击,可选中, 可编辑】
     QTreeWidgetItem *ptr = new QTreeWidgetItem;
     if (col == 1)
         ptr->setFlags(ptr->flags()|Qt::ItemIsEditable); //可编辑
    else
        ptr->setFlags(ptr->flags()&~(Qt::ItemIsEditable)); //不可编辑
    
     ptr->setFlags(ptr->flags()&~(Qt::ItemIsEnabled)); //视图中会灰掉且不可编辑
    
    
    1. QTreeWidget如何设置表头不可见
    QTreeWidget *m_pNewTree=new QTreeWidget();
    
    m_pNewTree->header()->setVisible(false); //隐藏表头
    
    1. 设置代理模式,鼠标点击QTreeWidgetItem, 变为QLineEdit后,若用户在输入状态,此时如果要鼠标移开窗口,自动变为编辑完状态,该怎么办呢?
      问题如下图: [鼠标移动到 1,自动确认编辑完成]

      48fab43c13ef9e5cf83b3a2d466f6b6.png
      解决方法如下图
      image.png
    2. QTreeWidget 的自带样式:

    image.png
    调用上面的static 函数,获取支持的 样式list,我的是 ("windowsvista", "Windows", "Fusion")
    然后直接调用 m_tree->setStyle(QStyleFactory::create("windowsvista"));就OK了
    样式图如下: 8d3d061723615033754873639b41ff0.png d8167d9757bc19dde8af68e30bf903c.png b76142ada5fd931bc59d21728d3aaf3.png

    2.QComboBox

    1. QComboBox 调用 clear() 程序挂掉的问题:

    1.如果我们写好了下面的信号槽:
    connect(m_combo, &QComboBox::currentTextChanged, this, ....); 2. 调用clear(),clear会触发TextChanged(), 貌似是删到最后一个的时候会造成数组越界; 解决方法如下:
    我们在调用clear()之前,先disconnect() -> clear() -> connect()

    image.png

    3. QLabel

    1. QLabel如何竖着显示文字: 可参考 https://www.pianshen.com/article/13631981868/

    4. 文件读写 QFile + QTextStream

    #include <QFile>
    #include <QTextStream>
    
    #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内置图标

    image.png
    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++;
            }
        }
    
    #用法1
     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 依赖的谷歌

    image.png

    各个event

      // 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 坐标

    1635392491(1).png

    相关文章

      网友评论

          本文标题:持续记录 Qt开发

          本文链接:https://www.haomeiwen.com/subject/bgechltx.html