gpt4 book ai didi

c++ - 小部件未正确显示

转载 作者:行者123 更新时间:2023-11-28 04:52:17 27 4
gpt4 key购买 nike

在这段代码中可以播放视频和播放*.mp3。代码工作正常,在我的 mainwindow.ui 中,我通过拖放添加了名为 widgetGif 的小部件。这个小部件也包含标签。但是当我运行程序时这个小部件不可见。如何显示这个名为 widgetGif

的小部件

这是我的部分代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMovie>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
vw = new QVideoWidget (this);
player->setVideoOutput(vw);
this->setCentralWidget(vw); //I think this is the reason

slider = new QSlider(this);
bar = new QProgressBar(this);
slider->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(slider);
ui->statusBar->addPermanentWidget(bar);



connect(player,&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
connect(player,&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
connect(slider,&QSlider::sliderMoved,player,&QMediaPlayer::setPosition);
connect(player,&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
connect(player,&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);


sliderVolumn = new QSlider(this);
sliderVolumn->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(sliderVolumn);

connect(sliderVolumn,&QSlider::sliderMoved,player,&QMediaPlayer::setVolume);

QMovie *movie=new QMovie(":/res/icons/giphy.gif");
if (!movie->isValid())
{
// Something went wrong :(
}
ui->labelGif->setMovie(movie);
movie->start();

ui->widgetGif->setVisible(true);
}


MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_actionOpen_triggered()
{
QString filename= QFileDialog::getOpenFileName(this,"Open Folder","","Open a File(*.*)");
on_actionStop_triggered();
player->setMedia(QUrl::fromLocalFile(filename));
on_actionPlay_triggered();

if(filename.endsWith(".mp3")){
qDebug() << " file is mp3";

}else{
qDebug() << " not is mp3";

}
}

最佳答案

QMainWindow 需要一个 centralWidget。您必须使用布局来包含您的QVideoWidget 以及您的widgetGif,然后将其设置为QMainWindowcentralWidget

在下面的示例中,textEdit 将是您的 QVideoWidget,对象 label 就像您的 widgetGif

我们还有两个按钮可以使用 setVisible 方法隐藏或显示 label

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class QLabel;
class QPushButton;
class QTextEdit;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
QWidget *centralWidget;
QLabel *label;
QPushButton *pushButton;
QPushButton *pushButton_2;
QTextEdit *textEdit;

public slots:
void showSlot();
void hideSlot();
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

// Vertical layout
QVBoxLayout *mainLayout = new QVBoxLayout;

// Widgets
centralWidget = new QWidget(this);
label = new QLabel();
pushButton = new QPushButton();
pushButton_2 = new QPushButton();
textEdit = new QTextEdit();

// Title and texts
this->setWindowTitle("MainWindow");
label->setText("TextLabel");
pushButton->setText("Show");
pushButton_2->setText("Hide");

// Add widgets to layout
mainLayout->addWidget(textEdit);
mainLayout->addWidget(label);
mainLayout->addWidget(pushButton);
mainLayout->addWidget(pushButton_2);

centralWidget->setLayout(mainLayout);

// Set the central widget
this->setCentralWidget(centralWidget);

connect(pushButton, SIGNAL (clicked()), this, SLOT (showSlot()));
connect(pushButton_2, SIGNAL (clicked()), this, SLOT (hideSlot()));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::showSlot()
{
label->setVisible(true);
}

void MainWindow::hideSlot()
{
label->setVisible(false);
}

关于c++ - 小部件未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47899834/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com