Qt项目中的常见用法

来源:互联网 发布:java instance 编辑:程序博客网 时间:2024/06/11 05:16
  1. 1.QWidget设置为类似如右键菜单  
  2. setWindowFlags(Qt::FramelessWindowHint | Qt::Popup | Qt::NoDropShadowWindowHint);  
  3.   
  4. 2.QTreeWidget创建parent item刷新问题  
  5. ui->treeRoom->resizeColumnToContents(0);  
  6. 3.  
  7. setWindowFlags(Qt::FramelessWindowHint | Qt::Popup | Qt::NoDropShadowWindowHint);  
  8. setAttribute(Qt::WA_NoSystemBackground, true);  
  9. setAttribute(Qt::WA_TranslucentBackground, true);  
  10. setAttribute(Qt::WA_DeleteOnClose);  
  11.   
  12. bool eventFilter(QObject *obj, QEvent *e){  
  13.     if(obj == this){  
  14.         if(QEvent::WindowDeactivate == e->type()){  
  15.             close();  
  16.             e->accept();  
  17.             return true;  
  18.         }  
  19.     }  
  20.     return QWidget::eventFilter(obj, e);  
  21. }  
  22.   
  23. 4.QTextCursor insertImage相同图片只显示一个,解决:  
  24. QTextImageFormat imageFormat;  
  25. imageFormat.setName(imagePath);  
  26. this->textCursor().insertImage(imageFormat, QTextFrameFormat::InFlow);  
  27. 5.设置背景色  
  28. QPalette palette = this->palette();  
  29. palette.setBrush(QPalette::Background, QBrush(QColor(0, 123, 122)));  
  30. this->setPalette(palette);  
  31. this->setAutoFillBackground(true);     
  32. paintEvent:  
  33. QPainter painter(this);  
  34. painter.setBrush(QColor("#1e1e21"));  
  35. painter.setPen(Qt::NoPen);  
  36. painter.drawRect(this->rect());  
  37. 6.设置QTextEdit可输入个数:  
  38. void MyWidget::slotTextChanged()  
  39. {  
  40.     if (0 == m_count) {  
  41.         return;  
  42.     }  
  43.   
  44.     QString curText = this->toPlainText();  
  45.     int len = curText.count();  
  46.     if (len > m_count) {  
  47.         int pos = this->textCursor().position();  
  48.         QTextCursor textCursor = this->textCursor();  
  49.         curText.remove(pos - (len - m_count), len - m_count);  
  50.         this->setText(curText);  
  51.         textCursor.setPosition(pos - (len - m_count));  
  52.         this->setTextCursor(textCursor);  
  53.     }  
  54. }  
  55. 7.QLineEdit设置搜索图标和清除按钮  
  56.     searchAction = new QAction(QIcon(qutil::skin("icon_search")), ""this);  
  57.     m_lineEdit->addAction(searchAction, QLineEdit::LeadingPosition);  
  58.     m_lineEdit->setClearButtonEnabled(true);  
  59.     QAction *clearAction = m_lineEdit->findChild<QAction*>(QLatin1String("_q_qlineeditclearaction"));  
  60.     if (clearAction) {  
  61.         clearAction->setIcon(QIcon(":/xyz");  
  62.         connect(clearAction, &QAction::triggered, [this] () {  
  63.             emit sigClear();  
  64.         });  
  65.     }  
  66. 8.水平线  
  67.     QFrame *line = new QFrame(this);  
  68.     line->setMaximumSize(QSize(16777215, 1));  
  69.     line->setStyleSheet(QStringLiteral("background:rgb(68, 70, 73);"));  
  70.     line->setFrameShape(QFrame::HLine);  
  71.     line->setFrameShadow(QFrame::Sunken);  
  72. 9.使用全局热键QxtGlobalShortcut  
  73.     if (!m_gsCaptureScreen->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_A))) {  
  74.         /////////////  
  75.     }  
  76. 10.改变showNormal之后的大小setGeometry  
  77. 11.无title窗口拉伸(windows环境)  
  78. bool MyWindow::nativeEvent(const QByteArray & eventType, void * message, long * result)  
  79. {  
  80.     Q_UNUSED(eventType);  
  81.     if (this->isMaximized()) {  
  82.         return false;  
  83.     }  
  84.   
  85.     const int HIT_BORDER = 5;  
  86.     const MSG *msg=static_cast<MSG*>(message);  
  87.     if(msg->message == WM_NCHITTEST) {  
  88.         int xPos = ((int)(short)LOWORD(msg->lParam)) - this->frameGeometry().x();  
  89.         int yPos = ((int)(short)HIWORD(msg->lParam)) - this->frameGeometry().y();  
  90.         if(this->childAt(xPos,yPos) == 0) {  
  91.             *result = HTCAPTION;  
  92.         } else {  
  93.             return false;  
  94.         }  
  95.         if(xPos > 0 && xPos < HIT_BORDER) {  
  96.             *result = HTLEFT;  
  97.         }  
  98.         if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0)) {  
  99.             *result = HTRIGHT;  
  100.         }  
  101.         if(yPos > 0 && yPos < HIT_BORDER) {  
  102.             *result = HTTOP;  
  103.         }  
  104.         if(yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {  
  105.             *result = HTBOTTOM;  
  106.         }  
  107.         if(xPos > 0 && xPos < HIT_BORDER && yPos > 0 && yPos < HIT_BORDER) {  
  108.             *result = HTTOPLEFT;  
  109.         }  
  110.         if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos > 0 && yPos < HIT_BORDER) {  
  111.             *result = HTTOPRIGHT;  
  112.         }  
  113.         if(xPos > 0 && xPos < HIT_BORDER && yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {  
  114.             *result = HTBOTTOMLEFT;  
  115.         }  
  116.         if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {  
  117.             *result = HTBOTTOMRIGHT;  
  118.         }  
  119.         return true;  
  120.     }  
  121.     return false;  
  122. }  
  123. 12.安装全局事件过滤器  
  124. class GlobalEventFilter : public QObject  
  125. {  
  126.     Q_OBJECT  
  127. public:  
  128.     GlobalEventFilter(QObject *parent);  
  129.   
  130. protected:  
  131.     bool eventFilter(QObject *, QEvent *);  
  132. };  
  133. GlobalEventFilter *globalEventFilter = new GlobalEventFilter(this);  
  134. qApp->installEventFilter(globalEventFilter);  
  135. 13.QTreeWidget遍历  
  136. QTreeWidget *myTree;  
  137. QTreeWidgetItemIterator iter(myTree);  
  138. while (*iter) {  
  139.     ++iter;  
  140. }  
  141. 14.这里需要特别注意一点,如果QWidget直接show出来,是有背景色的,  
  142. 但是如果它作为一个父QWidget的子窗口时就没有背景了!此时需要添加如下代码:  
  143.     this->setAutoFillBackground(true);  
  144.     QPalette palette = this->palette();  
  145.     palette.setColor(QPalette::Background, QColor("#36383D"));  
  146.     this->setPalette(palette);  
  147. 15.模态对话框  
  148.     QEventLoop eventLoop;  
  149.     myWidget->setAttribute(Qt::WA_ShowModal, true);  
  150.     myWidget->show();  
  151.     connect(myWidget, &QObject::destroyed,  &eventLoop, &QEventLoop::quit, Qt::DirectConnection);  
  152.     eventLoop.exec();  
  153. 16.QTreeWidget删除item  
  154.     QTreeWidgetItemIterator iter(ui->treeRoom);  
  155.     while (*iter) {  
  156.         delete item;  
  157.     }  
  158. 17.窗口抖动  
  159. void Shake::start(QWidget *targetWidget, int number, int range)  
  160. {  
  161.     Q_ASSERT(NULL != targetWidget);  
  162.     m_widget = targetWidget;  
  163.     m_number = number;  
  164.     m_range = range;  
  165.     m_start = 0;  
  166.     m_point = m_widget->pos();  
  167.   
  168.     m_widget->raise();  
  169.     m_widget->activateWindow();  
  170.     bool isMaximized = m_widget->isMaximized();  
  171.     if (NULL == m_timer)  
  172.     {  
  173.         m_timer = new QTimer(this);  
  174.         QObject::connect(m_timer, &QTimer::timeout, [this, isMaximized] {  
  175.             if (m_start < m_number * 4)  
  176.             {  
  177.                 ++m_start;  
  178.                 switch (m_start % 4)  
  179.                 {  
  180.                 case 1:  
  181.                     {  
  182.                         QPoint newPos(m_point.x(), m_point.y() - m_range);  
  183.                         m_widget->move(newPos);  
  184.                     }  
  185.                     break;  
  186.   
  187.                 case 2:  
  188.                     {  
  189.                         QPoint newPos(m_point.x() - m_range, m_point.y() - m_range);  
  190.                         m_widget->move(newPos);  
  191.                     }  
  192.                     break;  
  193.   
  194.                 case 3:  
  195.                     {  
  196.                         QPoint newPos(m_point.x() - m_range, m_point.y());  
  197.                         m_widget->move(newPos);  
  198.                     }  
  199.                     break;  
  200.   
  201.                 default:  
  202.                     m_widget->move(m_point);  
  203.                     break;  
  204.                 }  
  205.             } else {  
  206.                 m_timer->stop();  
  207.             }  
  208.         });  
  209.     }  
  210.     m_timer->start(40);  
  211. }  

18.如果刷新有问题可以调用:Widget->style()->polish(this);

19. QComboBox 对齐
 // First : Set the combobox the editable (this allows us to use the lineEdit)
  mComboBox->setEditable(true);
  // Second : Put the lineEdit in read-only mode
  mComboBox->lineEdit()->setReadOnly(true);
  // Third  : Align the lineEdit to right
  mComboBox->lineEdit()->setAlignment(Qt::AlignRight);  
  // Fourth : Align each item in the combo to the right
  for(int i = 0; i < mComboBox->count(); i++)
    mComboBox->setItemData(i, Qt::AlignRight, Qt::TextAlignmentRole);

20.// 去掉输入法输入时的虚线,但是会导致输入法输入框位置不正确
void TextEdit::inputMethodEvent(QInputMethodEvent *e)
{
    if (e->commitString().isEmpty() && !e->preeditString().isEmpty()) {
        return;
    }
    QTextEdit::inputMethodEvent(e);
}

0 0