zoukankan      html  css  js  c++  java
  • 【QT】视频播放+文件选择

    折腾了两个小时,太久没用了,找了半天的感觉。

    先是在视频播放 的代码基础上加选择视频的按钮,开始总是显示两个框,后来发现需要用QSplitter来实现同时有多个框的情况。

    把中心窗口设为这个splitter就可以了:setCentralWidget(splitter); 注意,这里不能用layout.

    再然后是槽, 我直接写slots: 就报错, 写 private slots:就是对的。 注意槽函数一定要声明在slots里。

    文件选择用QFileDialog::getOpenFileName();

    每个控件都是在类里面先声明一个相应的指针,然后在函数中具体分配设置。信号与槽的连接也是在构造函数中的。

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow()
    {
        playlist = new QMediaPlaylist;
        player = new QMediaPlayer;
        videoWidget = new QVideoWidget;
    
        chooseVideoButton = new QPushButton(tr("Choose Video"));
        QSplitter * splitter = new QSplitter;
        splitter->addWidget(videoWidget);
        splitter->addWidget(chooseVideoButton);
    
        setCentralWidget(splitter);
    
        connect(chooseVideoButton, SIGNAL(clicked()), this, SLOT(getVideoName()));
    
    
        videoWidget->show();
    
    }
    
    void MainWindow::getVideoName()
    {
        fileName = QFileDialog::getOpenFileName(this,
            tr("Open Video"), "E:", tr("Video Files (*.avi)"));
        playlist->addMedia(QUrl::fromLocalFile(fileName));
        playlist->setCurrentIndex(1);
        player->setPlaylist(playlist);
        player->setVideoOutput(videoWidget);
        player->play();
    }
    
    MainWindow::~MainWindow()
    {
    
    }

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMediaPlayer>
    #include <QMediaPlaylist>
    #include <QVideoWidget>
    #include <QUrl>
    #include <QPushButton>
    #include <QFileDialog>
    #include <QLayout>
    #include <QSplitter>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    private slots:
        void getVideoName();
    public:
        QMediaPlaylist * playlist;
        QMediaPlayer * player;
        QVideoWidget * videoWidget;
    
        QString fileName;
        QPushButton * chooseVideoButton;
    
        MainWindow();
        ~MainWindow();
    
    };
    
    #endif // MAINWINDOW_H

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
  • 相关阅读:
    NO.6: 若不想编译器提供自动生成的函数,就应该明确拒绝
    NO.5: 了解C++编译器默认为你生成的构造/赋值/析构
    NO.4: 确定对象被使用前已被初始化
    NO.3: 尽量使用const
    NO.2: 尽量以const,enum,inline 替换 #define
    NO.1: 视C++为一个语言联邦
    C/C++ exception类
    C/C++ 类成员函数指针 类成员数据指针
    c++中的 Stl 算法(很乱别看)
    自定义类签发校验token-实现多方式登录-自定义反爬类-admin后台表管理字段自定义-群查接口-搜索-排序-分页
  • 原文地址:https://www.cnblogs.com/dplearning/p/4388443.html
Copyright © 2011-2022 走看看