Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

c++ - Why is QSlider not updating it's position immediately in its UI?

I am creating a video application.

Upon starting this app, you would see a VideoWidget looping a playlist along with other widgets in the screen. By clicking the VideoWidget, the VideoWidget will go to fullscreen mode and a volume slider will be laid over it. It would go to show normal if clicked again in fullscreen mode.

To do this I created 2 classes. First, I created a main class that would contain all widgets including the Video widget. Second, I created a custom VideoWidget class. I instatiated my Qslider in this VideoWidget class and I instantiated a VideoWidget Object in my main class whose object is instantiated in main.cpp.

I got what I expect it to do. Except that the slider would not update its position immediately. It would only update position if you click to show normal then click to go back fullscreen. The volume change but the position of slider in UI does not change while in fullscreen.

I would like to ask what am I doing wrong? What should I do so that the slider position would update in UI?

Code Snippet: in VideoWidget.h

class VideoWidget : public QVideoWidget
{
Q_OBJECT
QVideoWidget* videoWidget;

QMediaPlaylist* playlist;
QMediaPlayer *player;
public:
VideoWidget();
QSlider* slider;
};

In VideoWidget.cpp

VideoWidget::VideoWidget()
: videoWidget(new QVideoWidget(this)),
  slider(new QSlider(Qt::Horizontal, this))
{
/*QMediaplaylist *playlist, QMediaPlayer *player instantiated here*/
  slider->hide();
  slider->setGeometry(300,735,600,20);
  slider->setRange(0, 100);
  slider->setValue(player->volume());

  connect(slider, &QSlider::valueChanged, player, &QMediaPlayer::setVolume);
 }

void VideoWidget::changeEvent(QEvent *event)
{
if(event->type() == QEvent::WindowStateChange)
    slider->setVisible(windowState() == Qt::WindowFullScreen);
QWidget::changeEvent(event);

}
enter code here
void VideoWidget::resizeEvent(QResizeEvent* event) {
videoWidget->resize(size());
event->accept();
}

void VideoWidget::mousePressEvent(QMouseEvent *event)
{
this->setFullScreen(!isFullScreen());
event->accept();
}

In the MainWidget.cpp

mainwidget::mainwidget(QWidget *parent)
: QWidget(parent)

{
 videoWidget = new VideoWidget(); // the video container
 videoWidget->setFixedSize(500, 300);

 QBoxLayout *displayLayout = new QHBoxLayout;
 displayLayout->addWidget(videoWidget, 2);

 QBoxLayout *layout = new QVBoxLayout;
 layout->addLayout(displayLayout);
 setLayout(layout);

 videoWidget->setGeometry(100,100,300,400);
 videoWidget->show();
}

edit: This is the app at startup playing a video of my hand. enter image description here

When I click the Video, enter image description here

The video sets to fullscreen and the slider appears. The slider can control the volume of mediaplayer but the problem is, it won't move when dragged.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're very confusing in ways you describe what the problem is, but if I get you right that QSlider doesn't track the mouse but you can change volume with it, presumably with a click?

You had connected valueChangedsignal , which is emitted only after sliderReleased() if tracking property is false. You have to handle PressedMovedReleased group of signals if you want to adjust volume continuously, or you can use built-in function of QSlider (which usually is enough):

slider->setTracking(true);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...