OS : windows xp SP2 ,
compiler : Code::Blocks ver. 10.05 ,
Qt 4.6
I recently started to learn Qt. At first all went well with simple tut examples.
I soon came across an example that can not compile and and realized that something is wrong.
Here is the code :
#include <QWidget>
#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QDesktopWidget>
class Communicate : public QWidget
{
Q_OBJECT
public:
Communicate(QWidget *parent = 0);
private slots:
void OnPlus();
void OnMinus();
private:
QLabel *label;
};
void center(QWidget *widget, int w, int h)
{
int x, y;
int screenWidth;
int screenHeight;
QDesktopWidget *desktop = QApplication::desktop();
screenWidth = desktop->width();
screenHeight = desktop->height();
x = (screenWidth - w) / 2;
y = (screenHeight - h) / 2;
widget->move( x, y );
}
Communicate::Communicate(QWidget *parent)
: QWidget(parent)
{
int WIDTH = 350;
int HEIGHT = 190;
resize(WIDTH, HEIGHT);
QPushButton *plus = new QPushButton("+", this);
plus->setGeometry(50, 40, 75, 30);
QPushButton *minus = new QPushButton("-", this);
minus->setGeometry(50, 100, 75, 30);
label = new QLabel("0", this);
label->setGeometry(190, 80, 20, 30);
connect(plus, SIGNAL(clicked()), this, SLOT(OnPlus()));
connect(minus, SIGNAL(clicked()), this, SLOT(OnMinus()));
center(this, WIDTH, HEIGHT);
}
void Communicate::OnPlus()
{
int val = label->text().toInt();
val++;
label->setText(QString::number(val));
}
void Communicate::OnMinus()
{
int val = label->text().toInt();
val--;
label->setText(QString::number(val));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Communicate window;
window.setWindowTitle("Communicate");
window.show();
return app.exec();
}
When I try to open it, I get this message:
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| more undefined references to `vtable for Communicate' follow|
||=== Build finished: 6 errors, 0 warnings ===|
I was looking for a solution to the code:: blocks forum and learned that there should be Qt plugin installed.
So , I install QtWorkbench 0.6.0 alpha -> qt plugin but nothing has changed.
Any suggestion is welcome.
Did you moc this file and include the moc output to get compiled?
Whenever you use the Q_OBJECT macro, you must use Qt's moc command on that file to generate a new cpp file which should also be included in the files to be compiled with your project. In addition, I believe that you can only moc a header file, so you will have to move your class definition to a separate file and moc that file.
I don't know how it works for your IDE, but on the command line you would call something like
<QT4 directory>\bin\moc.exe myfile.h -o moc_myfile.cpp
Then include the file moc_myfile.cpp in the project as well.
In some IDEs that is what the Qt plugin does for you; It automates all those steps or uses qmake which does not require explicit moc'ing. In Visual Studio I just use Custom Build Steps.
Try taking out the call to center() in the Communication constructor. I believe this might be causing your errors.
Related
I have derived the class of QTabBar to implement "+" (new tab button) button using QToolButton (similar to google chrome). However, it is working in my Linux machine but doesn't work in my windows machine. By not working I mean QToolButton is not visible in my windows machine but it is visible in my Linux machine (Ubuntu). I am not able to debug it further as I have tried few experiments to understand the reason but it didn't work.
My Source file:
#include "tabbar.h"
TabBar::TabBar(QWidget *parent) : QTabBar(parent)
{
new_button_ = new QToolButton(this);
new_button_->setObjectName(QStringLiteral("AddButton"));
new_button_->setText("+");
new_button_->setFixedSize(QSize(20, 20));
connect(new_button_, SIGNAL(released()), this, SLOT(emit_new()));
movePlusButton();
}
QSize TabBar::sizeHint(void) const
{
QSize old = QTabBar::sizeHint();
return QSize(old.width() + 45, old.height());
}
void TabBar::emit_new(void)
{
emit newClicked();
}
void TabBar::movePlusButton(void)
{
quint64 totalWidth = 0;
for (long i=0; i < count(); i++)
totalWidth += tabRect(i).width();
quint64 h = geometry().top();
quint64 tab_height = height();
quint64 w = width();
if (totalWidth > w)
new_button_->move(w-40, tab_height - 30);
else
new_button_->move(totalWidth + 5, tab_height - 30);
}
void TabBar::resizeEvent(QResizeEvent *p_evt)
{
QTabBar::resizeEvent(p_evt);
movePlusButton();
}
void TabBar::tabLayoutChange(void)
{
QTabBar::tabLayoutChange();
movePlusButton();
}
My Header File:
#ifndef TABBAR_H
#define TABBAR_H
#include <QObject>
#include <QToolButton>
#include <QTabBar>
#include <QResizeEvent>
#include <QLabel>
class TabBar : public QTabBar {
Q_OBJECT
public:
TabBar(QWidget *parent=nullptr);
~TabBar() { }
void movePlusButton(void);
void resizeEvent(QResizeEvent *p_evt) override;
void tabLayoutChange(void) override;
QSize sizeHint(void) const override;
private slots:
void emit_new(void);
signals:
void newClicked(void);
private:
QToolButton *new_button_;
};
#endif // TABBAR_H
EDIT:
I have tried few more experiments and got to know QToolButton is hiding behind region next to Tab bars. Please refer the screenshot.
Apparently, your application uses a stylesheet or something to customize the display and this is incompatible with your TabBar class.
Downloaded your code, wrote a simple main:
#include <QApplication>
#include <QMainWindow>
#include "tabbar.h"
int main( int argc, char* argv[] )
{
QApplication app(argc, argv);
QMainWindow wnd;
TabBar* tabBar = new TabBar(&wnd);
wnd.setCentralWidget( tabBar );
tabBar->addTab( "Foo" );
wnd.show();
return app.exec();
}
compiled and executed on Windows and got that (tested classic and aero style):
So apparently your code is fine. However, if you customized the QTabBar rendering through a stylesheet (what I suspect when I see how it looks in your GUI), you may need to adapt yourcode (probably movePlusButton as it has some values hardcoded that may be incompatible with the current display style):
if (totalWidth > w)
new_button_->move(w-40, tab_height - 30);
else
new_button_->move(totalWidth + 5, tab_height - 30);
I have a Windows & Mac program that switches into full-screen mode on multiple monitors. In Qt 4, it seems (I can't find explicit documentation on how to do this) like the 'correct' way to go about this is by creating N QMainWindow's for the N monitors on the machine, calling QWidget::move() to the N monitor's top-left x,y coordinates, and then calling QWidget::setWindowState(Qt::WindowFullScreen). I don't know whether this is The Right Thing To Do - again, I can't find any documentation or examples anywhere that do this in Qt.
This seems to be 'broken' (if it was ever the Right Thing To Do in the first place) in Qt 5.4.1, especially on Windows 7. I'm still trying to isolate the problem, but it seems like the QMainWindows are dropping out of full-screen mode.
Just so I'm clear about this, what is the right way to do this? I found this forum post which seems to suggest that I should be setting the QScreen on the underlying QWindow objects that are held by the QMainWindows, but this doesn't seem to work in my tests. Here's an example program that I wrote:
app.h:
#include <vector>
#include <QObject>
class QMainWindow;
class app : public QObject
{
Q_OBJECT
public:
int run(int argc, char** argv);
public slots:
void add_window();
void remove_window();
void windows_go_to_screens();
void windows_go_to_screens_old();
void windows_go_to_primary_screen();
void windows_fullscreen();
void windows_nonfullscreen();
private:
QMainWindow * create_window(const char * id);
void init_menus( QMainWindow * w );
std::vector<QMainWindow *> m_windows;
};
app.cpp:
#include <assert.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <QObject>
#include <QMainWindow>
#include <QApplication>
#include <QMenubar>
#include <QAction>
#include <QScreen>
#include <QWindow>
#include <QLayout>
#include <QLabel>
#include <QStyle>
#include "app.h"
using namespace std;
int app::run(int argc, char** argv)
{
QApplication a(argc, argv);
QMainWindow * w = create_window("0");
m_windows.push_back(w);
w->show();
return a.exec();
}
void app::add_window()
{
static const char * nums[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
m_windows.push_back(create_window(nums[m_windows.size()]));
m_windows.back()->show();
}
void app::remove_window()
{
if (m_windows.size() > 1)
{
QMainWindow * w = m_windows.back();
m_windows.pop_back();
w->close();
w->deleteLater();
}
}
void app::windows_go_to_screens()
{
QList<QScreen*> screens = qApp->screens();
for (unsigned i = 0; i < std::min((unsigned)m_windows.size(), (unsigned)screens.size()); ++i)
{
QMainWindow * mw = m_windows[i];
QScreen * screen = screens[i];
QWindow * wh = mw->windowHandle();
wh->setScreen(screen);
}
}
void app::windows_go_to_screens_old()
{
QList<QScreen*> screens = qApp->screens();
for (unsigned i = 0; i < std::min((unsigned)m_windows.size(), (unsigned)screens.size()); ++i)
{
QMainWindow * mw = m_windows[i];
QScreen * screen = screens[i];
mw->move(screen->geometry().left(), screen->geometry().top());
}
}
void app::windows_go_to_primary_screen()
{
QList<QScreen*> screens = qApp->screens();
for (unsigned i = 0; i < std::min((unsigned)m_windows.size(), (unsigned)screens.size()); ++i)
{
QMainWindow * mw = m_windows[i];
QScreen * screen = screens[0];
QWindow * wh = mw->windowHandle();
wh->setScreen(screen);
}
}
void app::windows_fullscreen()
{
for (unsigned i = 0; i < m_windows.size(); ++i)
{
QMainWindow * mw = m_windows[i];
mw->showFullScreen();
}
}
void app::windows_nonfullscreen()
{
for (unsigned i = 0; i < m_windows.size(); ++i)
{
QMainWindow * mw = m_windows[i];
mw->showNormal();
}
}
QMainWindow * app::create_window(const char * id)
{
QMainWindow * w = new QMainWindow(NULL);
init_menus(w);
QWidget * cw = new QWidget(w);
w->setCentralWidget(cw);
QHBoxLayout * l = new QHBoxLayout(cw);
cw->setLayout(l);
QLabel * lab = new QLabel(id, cw);
QPalette pal(lab->palette());
pal.setColor(QPalette::Background, Qt::red);
lab->setAutoFillBackground(true);
lab->setPalette(pal);
lab->setScaledContents(true);
lab->setAlignment(Qt::AlignCenter);
l->addWidget( lab );
return w;
}
void app::init_menus( QMainWindow * w )
{
QMenuBar * menubar = w->menuBar();
QMenu * view_menu = new QMenu(tr("View"), w);
view_menu->addAction("Add Window", this, SLOT(add_window()));
view_menu->addAction("Remove Window", this, SLOT(remove_window()));
view_menu->addAction("Windows Go To Screens", this, SLOT(windows_go_to_screens()));
view_menu->addAction("Windows Go To Screens (old method)", this, SLOT(windows_go_to_screens_old()));
view_menu->addAction("Windows Go To Primary Screen", this, SLOT(windows_go_to_primary_screen()));
view_menu->addAction("Windows Fullscreen", this, SLOT(windows_fullscreen()));
view_menu->addAction("Windows Non-Fullscreen", this, SLOT(windows_nonfullscreen()));
menubar->addMenu(view_menu);
}
main.cpp:
#include "app.h"
int main(int argc, char** argv)
{
app a;
return a.run(argc, argv);
}
When I run this program on OS X, the "Windows Go To Screens" function does nothing - none of the windows move. Neither does the "Windows Go To Primary Screen" (poorly named - should be 0 screen?). Creating more than N windows on an N window Mac is interesting - in that case calling "Windows Fullscreen" several times will actually switch the QMainWindows into fullscreen mode one at a time?!
Even more interesting is what happens on a multi-monitor OS X machine when you do this: "Add Window" until you have as many windows as displays. "Windows Go To Screens (old method)" will send each window to the top-left of each monitor. "Windows Fullscreen" will make all windows go full-screen on all monitors. "Remove Window" until you have only 1 window left. Then "Windows Non-FullScreen", and you'll get an interesting surprise. Go into Mission Control to see what's going on.
Can anyone tell me what the RIGHT way of doing this is? I've looked through the Qt5 examples - there's the player application that seems to be thoroughly broken (it can play a video in full-screen mode once, and then subsequent plays are in a separate desktop window), the sub game only maximizes to a single display, and none of the other examples seem to utilize full-screen mode, and certainly not on multiple monitors.
One way of doing it in Qt5 is to use QWindow::setScreen to set the screen on which the window should be shown. QWidget has a windowHandle() that returns the pointer to the QWindow. So you can get that pointer for each window and set a different screen.
Here is how to show your widget in the last screen in full-screen mode :
QWidget * widget = new QWidget();
widget->show();
widget->windowHandle()->setScreen(qApp->screens().last());
widget->showFullScreen();
Or in the second screen :
QWidget * widget = new QWidget();
widget->show();
widget->windowHandle()->setScreen(qApp->screens()[1]);
widget->showFullScreen();
I want to try QWinThumbnailToolBar in Qt 5.2 but it doesn't work !(Program runs but there is no thumbnail !!!!)
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWinThumbnailToolButton>
#include <QWinThumbnailToolBar>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QWinThumbnailToolBar* thumbnailToolBar;
QWinThumbnailToolButton *playToolButton;
QWinThumbnailToolButton *forwardToolButton;
QWinThumbnailToolButton *backwardToolButton;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
thumbnailToolBar = new QWinThumbnailToolBar(this);
thumbnailToolBar->setWindow(this->windowHandle());
playToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
playToolButton->setEnabled(false);
playToolButton->setToolTip(tr("true"));
playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
forwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
forwardToolButton->setEnabled(true);
forwardToolButton->setToolTip(tr("Fast forward"));
forwardToolButton->setIcon(style()->standardIcon(QStyle::SP_TrashIcon));
backwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
backwardToolButton->setEnabled(true);
backwardToolButton->setToolTip(tr("Rewind"));
backwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekBackward));
thumbnailToolBar->addButton(backwardToolButton);
thumbnailToolBar->addButton(playToolButton);
thumbnailToolBar->addButton(forwardToolButton);
}
MainWindow::~MainWindow()
{
delete ui;
}
//pro file :
QT += core gui winextras multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
I think the problem is with these two line :
thumbnailToolBar = new QWinThumbnailToolBar(this);
thumbnailToolBar->setWindow(this->windowHandle());
I also tried to use QWidget instead of QMainWindow...
How can I fix it ??
Your code to create QWinThumbnailToolBar is correct, the problem is where you create it. I think creating it in the window constructor is the problem (Maybe because the window handle is not ready yet). You can make something like this:
// main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FrmMain w;
w.show();
w.createThmbBar();
return a.exec();
}
Where createThumbBar() is a public function where you create the QWinThumbnailToolBar as in:
// MainWindow.cpp
void MainWindow::createThmbBar()
{
thumbnailToolBar = new QWinThumbnailToolBar(this);
thumbnailToolBar->setWindow(this->windowHandle());
playToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
playToolButton->setEnabled(false);
playToolButton->setToolTip(tr("true"));
playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
forwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
forwardToolButton->setEnabled(true);
forwardToolButton->setToolTip(tr("Fast forward"));
forwardToolButton->setIcon(style()->standardIcon(QStyle::SP_TrashIcon));
backwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar);
backwardToolButton->setEnabled(true);
backwardToolButton->setToolTip(tr("Rewind"));
backwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekBackward));
thumbnailToolBar->addButton(backwardToolButton);
thumbnailToolBar->addButton(playToolButton);
thumbnailToolBar->addButton(forwardToolButton);
}
Late answer but hopefully it could help anyone faces the same issue later.
The fix that #Ramez proposed works however I am getting a crash on application shutdown due to the windows extra. Is there something special I need to do myself in the destructor? The QWindow has already been deleted when the QWinThumbnailToolBarPrivate::hasHandle() checks for the handle.
Exception thrown: read access violation.
d was 0xFFFFFFFFFFFFFF7F.
QPlatformWindow *QWindow::handle() const
{
Q_D(const QWindow);
return d->platformWindow;
}
Stack Trace Below:
Qt5Guid.dll!QWindow::handle() Line 1929 C++
Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::hasHandle() Line 460 C++
Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::handle() Line 465 C++
Qt5WinExtrasd.dll!QWinThumbnailToolBarPrivate::nativeEventFilter(const QByteArray & __formal, void * message, long * result) Line 549 C++
Qt5Cored.dll!QAbstractEventDispatcher::filterNativeEvent(const QByteArray & eventType, void * message, long * result) Line 484 C++
[External Code]
Qt5Guid.dll!QWindowPrivate::destroy() Line 1914 C++
Qt5Guid.dll!QWindow::destroy() Line 1864 C++
Qt5Widgetsd.dll!QWidgetPrivate::deleteTLSysExtra() Line 1891 C++
Qt5Widgetsd.dll!QWidget::destroy(bool destroyWindow, bool destroySubWindows) Line 12515 C++
Qt5Widgetsd.dll!QApplication::~QApplication() Line 798 C++
Hey stackies currently trying to create some basic code that just plays a video file from local source (as trying to get that working then try and implement rtsp). below is my current code. If anyone can give me an idea where im going wrong that would be great.
Currently the code is failing with the error
C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:58: error: C2664: 'QGraphicsScene::addItem' : cannot convert parameter 1 from 'QGraphicsVideoItem *' to 'QGraphicsItem *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
I presume I have to typecast the variable being inserted to additem. However have no idea how to do this with two Qclasses. If someone has an example of a basic player that just contains a video frame and plays the second you start the program ( not even a play button preferably). If anyone has anything like that please let me know as would make my life alot easier.
Main.cpp
#include "videoplayer.h"
#include <QApplication>
#include <QGraphicsView>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
//VideoPlayer player;
//QGraphicsView graphicsView;
QMediaPlayer *player ;
QGraphicsVideoItem *item;
QGraphicsView *graphicsView;
QGraphicsScene *scene;
player->setVideoOutput(item);
// scene->addItem(item);
graphicsView->scene()->addItem(item);
graphicsView->show();
player->setMedia(QUrl("rtsp://192.168.100.58:8554/stream"));
player->play();
graphicsView->show();
return app.exec();
}
Videoplayer.cpp
#include "videoplayer.h"
#include <QtWidgets>
#include <QVideoSurfaceFormat>
#include <QGraphicsVideoItem>
VideoPlayer::VideoPlayer(QWidget *parent)
: QWidget(parent)
, mediaPlayer(0, QMediaPlayer::VideoSurface)
, videoItem(0)
, playButton(0)
, positionSlider(0)
{
videoItem = new QGraphicsVideoItem;
videoItem->setSize(QSizeF(640, 480));
QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsView *graphicsView = new QGraphicsView(scene);
scene->addItem(videoItem);
QSlider *rotateSlider = new QSlider(Qt::Horizontal);
rotateSlider->setRange(-180, 180);
rotateSlider->setValue(0);
connect(rotateSlider, SIGNAL(valueChanged(int)),
this, SLOT(rotateVideo(int)));
QAbstractButton *openButton = new QPushButton(tr("Open..."));
connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
playButton = new QPushButton;
playButton->setEnabled(false);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(playButton, SIGNAL(clicked()),
this, SLOT(play()));
positionSlider = new QSlider(Qt::Horizontal);
positionSlider->setRange(0, 0);
connect(positionSlider, SIGNAL(sliderMoved(int)),
this, SLOT(setPosition(int)));
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(openButton);
controlLayout->addWidget(playButton);
controlLayout->addWidget(positionSlider);
QBoxLayout *layout = new QVBoxLayout;
layout->addWidget(graphicsView);
layout->addWidget(rotateSlider);
layout->addLayout(controlLayout);
setLayout(layout);
mediaPlayer.setVideoOutput(videoItem);
connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(mediaStateChanged(QMediaPlayer::State)));
connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
}
VideoPlayer::~VideoPlayer()
{
}
void VideoPlayer::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
//QString fileName = "rtsp://192.168.100.58:8554/stream";
if (!fileName.isEmpty()) {
mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));
playButton->setEnabled(true);
}
}
void VideoPlayer::play()
{
switch(mediaPlayer.state()) {
case QMediaPlayer::PlayingState:
mediaPlayer.pause();
break;
default:
mediaPlayer.play();
break;
}
}
Videoplayer.h
#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H
#include <QMediaPlayer>
#include <QMovie>
#include <QWidget>
class QAbstractButton;
class QSlider;
class QGraphicsVideoItem;
class VideoPlayer : public QWidget
{
Q_OBJECT
public:
VideoPlayer(QWidget *parent = 0);
~VideoPlayer();
QSize sizeHint() const { return QSize(800, 600); }
public slots:
void openFile();
void play();
private slots:
void mediaStateChanged(QMediaPlayer::State state);
void positionChanged(qint64 position);
void durationChanged(qint64 duration);
void setPosition(int position);
void rotateVideo(int angle);
private:
QMediaPlayer mediaPlayer;
QGraphicsVideoItem *videoItem;
QAbstractButton *playButton;
QSlider *positionSlider;
};
#endif
.pro
TEMPLATE = app
TARGET = videographicsitem
QT += multimedia multimediawidgets
HEADERS += videoplayer.h
SOURCES += main.cpp \
videoplayer.cpp
target.path = $$[QT_INSTALL_EXAMPLES]/multimediawidgets/videographicsitem
INSTALLS += target
QT+=widgets
ALSO just tried this
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMediaPlayer media;
const QString file = "C:/Users/nick.herniman/Downloads/big_buck_bunny_1080p_h264.mov";
QUrl url(QFileInfo(file).absoluteFilePath());
media.setMedia(url);
media.play();
return a.exec();
}
which works but only plays the audio of the file anyone know what im doing wrong here (does it need to be in a gui window or is it ok for me to just do it in a console application)?
There is nothing weird. :-)
It helps, when the compiler knows, that a QGraphicsVideoItem is a QGraphicsItem. And for the compiler to know this, #include <QGraphicsVideoItem> should be present.
I see, you have it in Videoplayer.cpp. But main.cpp sees only that there is a class QGraphicsVideoItem declared. You did a fine forward declaration. But what this class is... no way to know for the compiler in main.cpp. Add #include <QGraphicsVideoItem> to your main.cpp.
Try this simplest Example in Qt5:
QMediaPlayer *mediaPlayer = new QMediaPlayer(this, QMediaPlayer::VideoSurface);
QGraphicsVideoItem *videoItem = new QGraphicsVideoItem;
videoItem->setPos(100,100);
QGraphicsScene *scene = new QGraphicsScene(this);
ui->GV_VideoPlayer->setScene(scene);
scene->addItem(videoItem);
QString fileName = "/root/sampleVideos/videoplayback.mp4";
if(!fileName.isEmpty())
{
mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
}
mediaPlayer->play();
mediaPlayer->setVideoOutput(videoItem);
It is actually really weird that the QT documentation states a very similar example ( http://qt-project.org/doc/qt-5.0/qtmultimedia/qgraphicsvideoitem.html ), while the objects are actually not related. Apart from that, just looking through the QT documentation since I haven't worked with QT for a while - does creating a dummy QGraphicsItem, setting it as a parent to your QGraphicsVideoItem and then adding that dummy item to the scene help? I guess it would go something like this
QGraphicsItem* dummy = new QGraphicsItem;
item = new QGraphicsVideoItem(dummy);
graphicsView->scene()->addItem(dummy);
codeblocks 8.02. , win xp SP2 , Qt 4.6
After installing Qt SDK, I installed QtWorkbench (codeblocks plugin that allows you to create Qt applications.) http://code.google.com/p/qtworkbench/.
I worked under instructions from that page. I opened the folder "dialogs" and in it I opened a new empty codeblocks project. Also in this folder "dialogs" I opened a new directory "complexwizard". In complexwizard is simple main.cpp :
#include <QWidget>
#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QDesktopWidget>
class Communicate : public QWidget
{
Q_OBJECT
public:
Communicate(QWidget *parent = 0);
private slots:
void OnPlus();
void OnMinus();
private:
QLabel *label;
};
void center(QWidget *widget, int w, int h)
{
int x, y;
int screenWidth;
int screenHeight;
QDesktopWidget *desktop = QApplication::desktop();
screenWidth = desktop->width();
screenHeight = desktop->height();
x = (screenWidth - w) / 2;
y = (screenHeight - h) / 2;
widget->move( x, y );
}
Communicate::Communicate(QWidget *parent)
: QWidget(parent)
{
int WIDTH = 350;
int HEIGHT = 190;
resize(WIDTH, HEIGHT);
QPushButton *plus = new QPushButton("+", this);
plus->setGeometry(50, 40, 75, 30);
QPushButton *minus = new QPushButton("-", this);
minus->setGeometry(50, 100, 75, 30);
label = new QLabel("0", this);
label->setGeometry(190, 80, 20, 30);
connect(plus, SIGNAL(clicked()), this, SLOT(OnPlus()));
connect(minus, SIGNAL(clicked()), this, SLOT(OnMinus()));
center(this, WIDTH, HEIGHT);
}
void Communicate::OnPlus()
{
int val = label->text().toInt();
val++;
label->setText(QString::number(val));
}
void Communicate::OnMinus()
{
int val = label->text().toInt();
val--;
label->setText(QString::number(val));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Communicate window;
window.setWindowTitle("Communicate");
window.show();
return app.exec();
}
Then I added, "main.cpp" in a blank project and all configured according to the instructions from that page.
When I started to compile the program, compiler always says:
* It seems that this project has not been built yet. Do you want to buid it now? *
I press yes an got this message :
Process terminated with status 2 (0 minutes, 0 seconds)
0 errors, 0 warnings
In the folder "dialogs" where is a project, new files are created:
complexwizard.pro
Makefile.complexwizard
Makefile.complexwizard.Debug
Makefile.complexwizard.Release
Since I am relatively new to the world of programming, compiler and other things, this does not tell me much.
Therefore, I ask someone who has some suggestion on the basis of these symptoms to help me remove it from standstill.
If you're interested, I'll add more data that will need
I 'm the author of QtWorkbench and I have stopped supporting it some time ago. I 'm pretty sure it's outdated by now. I really think that new Qt users should go with QtCreator the "official" Qt IDE to get the best support out of the box. QtWorkbench is still in Google Code in case any one wants to pick up developing it.