现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

Qt项目中的常见用法

2015-04-21 20:55 工业·编程 ⁄ 共 6032字 ⁄ 字号 暂无评论

1.QWidget设置为类似如右键菜单

setWindowFlags(Qt::FramelessWindowHint | Qt::Popup | Qt::NoDropShadowWindowHint);

2.QTreeWidget创建parent item刷新问题

ui->treeRoom->resizeColumnToContents(0);

3.

setWindowFlags(Qt::FramelessWindowHint | Qt::Popup | Qt::NoDropShadowWindowHint);

setAttribute(Qt::WA_NoSystemBackground, true);

setAttribute(Qt::WA_TranslucentBackground, true);

setAttribute(Qt::WA_DeleteOnClose);

bool eventFilter(QObject *obj, QEvent *e){

    if(obj == this){

        if(QEvent::WindowDeactivate == e->type()){

            close();

            e->accept();

            return true;

        }

    }

    return QWidget::eventFilter(obj, e);

}

4.QTextCursor insertImage相同图片只显示一个,解决:

QTextImageFormat imageFormat;

imageFormat.setName(imagePath);

this->textCursor().insertImage(imageFormat, QTextFrameFormat::InFlow);

5.设置背景色

QPalette palette = this->palette();

palette.setBrush(QPalette::Background, QBrush(QColor(0, 123, 122)));

this->setPalette(palette);

this->setAutoFillBackground(true);   

paintEvent:

QPainter painter(this);

painter.setBrush(QColor("#1e1e21"));

painter.setPen(Qt::NoPen);

painter.drawRect(this->rect());

6.设置QTextEdit可输入个数:

void MyWidget::slotTextChanged()

{

    if (0 == m_count) {

        return;

    }

    QString curText = this->toPlainText();

    int len = curText.count();

    if (len > m_count) {

        int pos = this->textCursor().position();

        QTextCursor textCursor = this->textCursor();

        curText.remove(pos - (len - m_count), len - m_count);

        this->setText(curText);

        textCursor.setPosition(pos - (len - m_count));

        this->setTextCursor(textCursor);

    }

}

7.QLineEdit设置搜索图标和清除按钮

    searchAction = new QAction(QIcon(qutil::skin("icon_search")), "", this);

    m_lineEdit->addAction(searchAction, QLineEdit::LeadingPosition);

    m_lineEdit->setClearButtonEnabled(true);

    QAction *clearAction = m_lineEdit->findChild<QAction*>(QLatin1String("_q_qlineeditclearaction"));

    if (clearAction) {

        clearAction->setIcon(QIcon(":/xyz");

        connect(clearAction, &QAction::triggered, [this] () {

            emit sigClear();

        });

    }

8.水平线

    QFrame *line = new QFrame(this);

    line->setMaximumSize(QSize(16777215, 1));

    line->setStyleSheet(QStringLiteral("background:rgb(68, 70, 73);"));

    line->setFrameShape(QFrame::HLine);

    line->setFrameShadow(QFrame::Sunken);

9.使用全局热键QxtGlobalShortcut

    if (!m_gsCaptureScreen->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_A))) {

        /////////////

    }

10.改变showNormal之后的大小setGeometry

11.无title窗口拉伸(windows环境)

bool MyWindow::nativeEvent(const QByteArray & eventType, void * message, long * result)

{

    Q_UNUSED(eventType);

    if (this->isMaximized()) {

        return false;

    }

    const int HIT_BORDER = 5;

    const MSG *msg=static_cast<MSG*>(message);

    if(msg->message == WM_NCHITTEST) {

        int xPos = ((int)(short)LOWORD(msg->lParam)) - this->frameGeometry().x();

        int yPos = ((int)(short)HIWORD(msg->lParam)) - this->frameGeometry().y();

        if(this->childAt(xPos,yPos) == 0) {

            *result = HTCAPTION;

        } else {

            return false;

        }

        if(xPos > 0 && xPos < HIT_BORDER) {

            *result = HTLEFT;

        }

        if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0)) {

            *result = HTRIGHT;

        }

        if(yPos > 0 && yPos < HIT_BORDER) {

            *result = HTTOP;

        }

        if(yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {

            *result = HTBOTTOM;

        }

        if(xPos > 0 && xPos < HIT_BORDER && yPos > 0 && yPos < HIT_BORDER) {

            *result = HTTOPLEFT;

        }

        if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos > 0 && yPos < HIT_BORDER) {

            *result = HTTOPRIGHT;

        }

        if(xPos > 0 && xPos < HIT_BORDER && yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {

            *result = HTBOTTOMLEFT;

        }

        if(xPos > (this->width() - HIT_BORDER) && xPos < (this->width() - 0) && yPos > (this->height() - HIT_BORDER) && yPos < (this->height() - 0)) {

            *result = HTBOTTOMRIGHT;

        }

        return true;

    }

    return false;

}

12.安装全局事件过滤器

class GlobalEventFilter : public QObject

{

    Q_OBJECT

public:

    GlobalEventFilter(QObject *parent);

protected:

    bool eventFilter(QObject *, QEvent *);

};

GlobalEventFilter *globalEventFilter = new GlobalEventFilter(this);

qApp->installEventFilter(globalEventFilter);

13.QTreeWidget遍历

QTreeWidget *myTree;

QTreeWidgetItemIterator iter(myTree);

while (*iter) {

    ++iter;

}

14.这里需要特别注意一点,如果QWidget直接show出来,是有背景色的,

但是如果它作为一个父QWidget的子窗口时就没有背景了!此时需要添加如下代码:

    this->setAutoFillBackground(true);

    QPalette palette = this->palette();

    palette.setColor(QPalette::Background, QColor("#36383D"));

    this->setPalette(palette);

15.模态对话框

    QEventLoop eventLoop;

    myWidget->setAttribute(Qt::WA_ShowModal, true);

    myWidget->show();

    connect(myWidget, &QObject::destroyed,  &eventLoop, &QEventLoop::quit, Qt::DirectConnection);

    eventLoop.exec();

16.QTreeWidget删除item

    QTreeWidgetItemIterator iter(ui->treeRoom);

    while (*iter) {

        delete item;

    }

17.窗口抖动

void Shake::start(QWidget *targetWidget, int number, int range)

{

    Q_ASSERT(NULL != targetWidget);

    m_widget = targetWidget;

    m_number = number;

    m_range = range;

    m_start = 0;

    m_point = m_widget->pos();

    m_widget->raise();

    m_widget->activateWindow();

    bool isMaximized = m_widget->isMaximized();

    if (NULL == m_timer)

    {

        m_timer = new QTimer(this);

        QObject::connect(m_timer, &QTimer::timeout, [this, isMaximized] {

            if (m_start < m_number * 4)

            {

                ++m_start;

                switch (m_start % 4)

                {

                case 1:

                    {

                        QPoint newPos(m_point.x(), m_point.y() - m_range);

                        m_widget->move(newPos);

                    }

                    break;

                case 2:

                    {

                        QPoint newPos(m_point.x() - m_range, m_point.y() - m_range);

                        m_widget->move(newPos);

                    }

                    break;

                case 3:

                    {

                        QPoint newPos(m_point.x() - m_range, m_point.y());

                        m_widget->move(newPos);

                    }

                    break;

                default:

                    m_widget->move(m_point);

                    break;

                }

            } else {

                m_timer->stop();

            }

        });

    }

    m_timer->start(40);

}

给我留言

留言无头像?