Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有重叠子窗口小部件的Qt自定义窗口小部件_C++_Qt_Widget_Qt5_Qwidget - Fatal编程技术网

C++ 具有重叠子窗口小部件的Qt自定义窗口小部件

C++ 具有重叠子窗口小部件的Qt自定义窗口小部件,c++,qt,widget,qt5,qwidget,C++,Qt,Widget,Qt5,Qwidget,我正在尝试在Qt5中制作一个自定义小部件,它有点像QProgressBar,但有3个滑块和用户可移动:稍微有点破损的JS实现: 我在想如何做到这一点时遇到了一些问题。我的尝试要么未能正确呈现小部件的所有部分,导致很难选择和移动小部件的不同部分,要么无法正确使用VBoxLayout,无法扩展以适应水平空间 我最近的一次尝试是,你应该能够从构造器那里得到大致的想法,没有实现其他任何东西 UTrackSlider::UTrackSlider(QWidget *parent) : QWidget(par

我正在尝试在Qt5中制作一个自定义小部件,它有点像QProgressBar,但有3个滑块和用户可移动:稍微有点破损的JS实现:

我在想如何做到这一点时遇到了一些问题。我的尝试要么未能正确呈现小部件的所有部分,导致很难选择和移动小部件的不同部分,要么无法正确使用VBoxLayout,无法扩展以适应水平空间

我最近的一次尝试是,你应该能够从构造器那里得到大致的想法,没有实现其他任何东西

UTrackSlider::UTrackSlider(QWidget *parent) : QWidget(parent)
{
    this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);

    // CIRCLE_HEIGHT = 10
    // BACKGROUND_HEIGHT = 30

    /// @todo css should be in global stylesheet
    background = new QLabel(this);
    background->setMinimumHeight(this->BACKGROUND_HEIGHT);
    background->setStyleSheet("QLabel{ border: 1px solid gray; background-color:white; border-radius:2px}");
    background->setAttribute(Qt::WA_DeleteOnClose);

    cue = new QLabel(this);
    cue->setFixedSize(QSize(this->CIRCLE_HEIGHT, this->CIRCLE_HEIGHT));
    cue->setStyleSheet("QLabel:hover{ border: 2px solid #d9534f; border-radius:5px}");
    cue->setAttribute(Qt::WA_DeleteOnClose);

    duration = new QLabel(this);
    duration->setFixedSize(3, this->BACKGROUND_HEIGHT);
    duration->setStyleSheet("QLabel{border: 3px solid #2376bb} QLabel:hover{border: 5px solid #2376bb}");
    duration->setAttribute(Qt::WA_DeleteOnClose);

    intro = new QLabel(this);
    intro->setFixedSize(QSize(this->CIRCLE_HEIGHT, this->CIRCLE_HEIGHT));
    intro->setStyleSheet("QLabel:hover{ border: 2px solid #5bc85c; border-radius:5px}");
    intro->setAttribute(Qt::WA_DeleteOnClose);

    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->addWidget(cue, 5, 0);
    mainLayout->addWidget(background, 0, this->CIRCLE_HEIGHT);
    mainLayout->addWidget(duration, 2, this->CIRCLE_HEIGHT);
    mainLayout->addWidget(intro, 5, this->CIRCLE_HEIGHT + this->BACKGROUND_HEIGHT);
    this->setLayout(mainLayout);
}
基本上,我应该如何构造这个复合小部件,使其在所有这些条件下工作,有什么建议吗


编辑:在与qt上的一些人讨论了这个问题之后,我得出结论,我必须覆盖这个事件。但这也意味着要覆盖一些其他函数来实现onClick和dragging效果,我不知道从哪里开始

你是对的,你必须重新实现

1.void paintEvent(QPaintEvent *) 
2.void mouseMoveEvent(QMouseEvent *)
3.void mousePressEvent(QMouseEvent *)
4.void mouseReleaseEvent(QMouseEvent *)
QWidget的一部分

为了正确处理鼠标事件,可以使用QCoreApplication::sendEventQObject*,QEvent*将事件传递给目标小部件

下面是一个例子:


在本例中,我在容器小部件中创建了三个widgetsColorSlider,然后使用链接列表正确传播鼠标事件。

Woah。这是你为我做的吗?太多了,是的!非常感谢: