my-QT专栏

QT 对于chart弹窗(scrollArea)中分屏

2021-09-17  本文已影响0人  c之气三段
   setAttribute(Qt::WA_DeleteOnClose);//调用close时析构自己,用于非模态
   ui->scrollArea->viewport()->installEventFilter(this);

多行两列

void SplitGraphDialog::addWidgets(const QList<QWidget*>& widgetList)
{   
    int colum = 0;
    addLine();
    foreach (auto var, widgetList)
    {
        if(colum>=m_columMax)
        {
            addLine();
            colum =0;
        }
        QWidget *widget = var;
        m_pHLayout->addWidget(widget);
        widget->setFixedSize(width()*0.5,height()*0.5);
        m_listWidget.append(widget);
        colum++;
    }
}

void SplitGraphDialog::addLine()
{
    m_pHLayout = new QHBoxLayout;
    m_pVLayout->addLayout(m_pHLayout);
}

//最大化时从设置大小
void SplitGraphDialog::changeEvent(QEvent*ev)
{
    if(ev->type()!=QEvent::WindowStateChange) return;
    if(this->windowState()==Qt::WindowMaximized)
    {
        foreach (auto var, m_listWidget)
        {
            QWidget *widget = var;
            widget->setFixedSize(width()*0.5,height()*0.5);
        }
    }
}

需求是在QScrollArea中有鼠标滚动事件,而qchart中有缩放事件。俩时间重合导致页面在下滚时,图标在缩放。解决方法是,当鼠标点在scrollAreaWidgetContents中时禁用 ui->scrollArea->viewport()的滚轮事件。否则的话释放。这样当鼠标移动到图表中就只会触发缩放事件,当鼠标出来就触发页面下滚事件。

bool SplitGraphDialog::eventFilter(QObject* watched, QEvent* event)
{
    if(watched == ui->scrollArea->viewport())
    {
        if(event->type() == QEvent::Wheel)
        {
            QMouseEvent * mouseEvent = static_cast<QMouseEvent * >(event);
            QWidget *myWidget= childAt(mouseEvent->pos());
            if(!(myWidget==ui->scrollAreaWidgetContents))
            {
                return true;
            }
        }
    }
    return false;
}
上一篇下一篇

猜你喜欢

热点阅读