Qt5 Video Player QtMultimedia QGraphicsVideoItem - 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);

Related

How to connect signal and slot with another object in qt --solved

I am doing a qt project, my goal is to draw something based on my input. The signal is in the main, and the drawing is in another object, I am not able to connect them.
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Frequency fre; //this is the class that does the drawing
QWidget *window = new QWidget;
QGridLayout *grid = new QGridLayout;
QGroupBox *groupBox = new QGroupBox(QObject::tr("Volume"));
QSpinBox *spinBox = new QSpinBox;
spinBox->setRange(0, 5);
QObject::connect(spinBox, SIGNAL(valueChanged(int)),&fre, SLOT(setVolume(int)));
QVBoxLayout *Vbox = new QVBoxLayout;
Vbox->addWidget(spinBox);
groupBox->setLayout(Vbox);
grid->addWidget(groupBox, 4,7,1,1);
window->setLayout(grid);
window->show();
return app.exec();
}
This is how I set up the Frequency class :
in h file
class Frequency : public QGLWidget
{
Q_OBJECT
public slots:
void setVolume(int value);
in cpp file
void Frequency :: setVolume(int val) {
vol = val;
updateGL();
}
void Frequency :: paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
}
Since I can't put all this in the comment section, I'm going to put it here:
I copied your code and I'm not able to reproduce the error with Qt version 5.14.0, because when I change the value in any way, the method, void MyObject::setVolume(int value) is being called.
This code should work for you. Possibly, you are running it in release mode, and used a breakpoint to check if it was called (which doesn't always work on release mode).
// #include "QtWidgetsApplication4.h"
#include <QtWidgets/QApplication>
#include "MyObject.h"
#include <QGridLayout>
#include <QGroupBox>
#include <QSpinBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// QtWidgetsApplication4 w;
// w.show();
// return a.exec();
MyObject object;
QWidget* window = new QWidget;
QGridLayout* grid = new QGridLayout;
QGroupBox* groupBox = new QGroupBox(QObject::tr("Volume"));
QSpinBox* spinBox = new QSpinBox;
spinBox->setRange(0, 5);
QObject::connect(spinBox, SIGNAL(valueChanged(int)), &object, SLOT(setVolume(int)));
QVBoxLayout* Vbox = new QVBoxLayout;
Vbox->addWidget(spinBox);
groupBox->setLayout(Vbox);
grid->addWidget(groupBox, 4, 7, 1, 1);
window->setLayout(grid);
window->show();
return a.exec();
}
#pragma once
#include <QObject>
class MyObject : public QObject
{
Q_OBJECT
public slots:
void setVolume(int value);
private:
int m_volume;
};
#include "MyObject.h"
void MyObject::setVolume(int value)
{
m_volume = value;
}

QMenu not execing at correct position first time

I have this very strange issue regarding a QMenu and its position when execing.
Here is the code for my subclassed QMenu:
DockItemContextMenu::DockItemContextMenu(QWidget *parent) : QMenu(parent){
style = qApp->style();
QPointer<QAction> restoreAction = new QAction(QIcon(style->standardIcon(QStyle::SP_TitleBarMaxButton)), "Restore", this);
QPointer<QAction> minimizeAction = new QAction(style->standardIcon(QStyle::SP_TitleBarMinButton), "Minimize", this);
QPointer<QAction> maximizeAction = new QAction(style->standardIcon(QStyle::SP_TitleBarMaxButton), "Maximize", this);
QPointer<QAction> stayOnTopAction = new QAction("Stay On Top", this);
stayOnTopAction->setCheckable(true);
QPointer<QAction> closeAction = new QAction(style->standardIcon(QStyle::SP_TitleBarCloseButton), "Close", this);
this->addActions({restoreAction, minimizeAction, maximizeAction, stayOnTopAction, closeAction});
connect(restoreAction, &QAction::triggered, parent, [this](){ emit restoreTriggered();}, Qt::QueuedConnection);
connect(minimizeAction, &QAction::triggered, parent, [this](){ emit minimizeTriggered();}, Qt::QueuedConnection);
connect(maximizeAction, &QAction::triggered, parent, [this](){ emit maximizeTriggered();}, Qt::QueuedConnection);
connect(stayOnTopAction, &QAction::triggered, parent, [this](){ emit stayOnTopTriggered();}, Qt::QueuedConnection);
connect(closeAction, &QAction::triggered, parent, [this](){ emit closeTriggered();}, Qt::QueuedConnection);
}
Okay, so essentially I have another widget who holds an instance of this DockItemContextMenu as a field. In this owning class, called Titlebar, I made it such that doing a right click will emit the customContextMenuRequested(QPoint) signal.
TitleBar::TitleBar(QString title, QWidget *parent){
...
this->setContextMenuPolicy(Qt::CustomContextMenu);
contextMenu = new DockItemContextMenu(this);
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)), Qt::QueuedConnection);
...
}
After this, this widget is essentially inserted into a QGraphicsScene and is converted implicitly into a QGraphicsItem. When I do the FIRST right click event on my Titlebar it will not exec at the correct screen position if I dragged the MainWindow of the entire QApplication anywhere other than its starting position on screen. In addition to being in a QGraphicsScene, this scene itself is always stored in a QSplitter. Now I would understand if this always had some sort of issue, but it turns out, every time I call the slot for that signal, ONLY the first time will it exec in the incorrect position in the QGraphicsScene. No matter how I manipulate the size of the Titlebar widget itself, move commands or maximize commands to the MainWindow, or even edit the splitter size for the QGraphicsView that affects the size of the QGraphicsScene, it will always be in the correct position afterwards. here is the function for execing:
void TitleBar::showContextMenu(QPoint point){
qDebug() << point;
contextMenu->exec(point);
emit _parent->focusChangedIn();
}
I printed the point at which it is calling the exec. The strangest part is that both times I right click in the same location, it will print the SAME value for the slot's positional parameter both the first exec and second exec, but be in the correct location every time other than the first. Did I forget to set some other flag when I added the context menu to the Titlebar class? Does it have anything to do with setting the QMenu's parent to the Titlebar? I'm just dumbfounded how the same QPoint could exec at two different screen locations given the same value. Does anybody have a clue what may or may not be happening on the first call to the Titlebar's slot for execing the QMenu?
EDIT: The issue stemmed from doing this line of code in the Titlebar constructor:
contextMenu = new DockItemContextMenu(this);
Changing it to:
contextMenu = new DockItemContextMenu;
fixed the issue. Does anyone know why, or is this possibly a bug? I rather not accept this as an answer because it does not explain why it happened in the first place.
Here is a minimal example with the same effect.
MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QGraphicsView>
#include <QSplitter>
#include <QHBoxLayout>
#include <QGraphicsScene>
#include <QPointer>
#include <QTreeWidget>
#include "titlebar.h"
class MainWindow : public QMainWindow{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
};
#endif // MAINWINDOW_H
MainWindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
QPointer<QWidget> widgetArea = new QWidget;
QPointer<QHBoxLayout> hLayout = new QHBoxLayout;
widgetArea->setLayout(hLayout);
QPointer<QSplitter> splitter = new QSplitter;
hLayout->addWidget(splitter);
QPointer<QTreeView> tree = new QTreeView;
splitter->addWidget(tree);
QPointer<QGraphicsView> view = new QGraphicsView;
splitter->addWidget(view);
splitter->setStretchFactor(0, 1);
splitter->setStretchFactor(1, 4);
QPointer<QGraphicsScene> scene = new QGraphicsScene;
view->setScene(scene);
QPointer<Titlebar> blue = new Titlebar;
blue->setObjectName("blue");
blue->setStyleSheet(QString("#blue{background-color: rgb(0,0,255)}"));
blue->resize(250,250);
scene->addWidget(blue);
this->setCentralWidget(widgetArea);
this->resize(1000,750);
}
MainWindow::~MainWindow(){
}
Titlebar.h:
#ifndef TITLEBAR_H
#define TITLEBAR_H
#include <QMenu>
#include <QWidget>
#include <QPointer>
#include <QDebug>
#include <QMouseEvent>
class Titlebar : public QWidget{
Q_OBJECT
public:
explicit Titlebar(QWidget *parent = nullptr);
QPointer<QMenu> menu;
QPoint currentPos;
protected slots:
void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void showContextMenu(QPoint point);
};
#endif // TITLEBAR_H
Titlebar.cpp:
#include "titlebar.h"
Titlebar::Titlebar(QWidget *parent) : QWidget(parent){
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)), Qt::QueuedConnection);
menu = new QMenu(this);
menu->addAction("Test");
}
void Titlebar::showContextMenu(QPoint point){
qDebug() << point;
menu->exec(mapToGlobal(point));
}
void Titlebar::mouseMoveEvent(QMouseEvent *event){
if (event->buttons() && Qt::LeftButton){
QPoint diff = event->pos() - currentPos;
move(pos() + diff);
}
}
void Titlebar::mousePressEvent(QMouseEvent * event){
currentPos = event->pos();
}
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
So this runs and reproduces the error accordingly. If you change the line in Titlebar.cpp from
menu = new QMenu(this);
to:
menu = new QMenu;
Then it works correctly. ONLY the first right click to open the context menu will spawn in the incorrect location on screen. All subsequent right clicks will now follow either the widget/window/splitter in any combination. I don't get it, can someone tell me if this is actually a bug or not.
You need to add one line of code because your using a QGraphicsProxyWidget which is part of a QGraphicsScene. The scene is represented by a QGraphicsView which inherits QAbstractScrollArea. This causes the context menu to be shown via the viewport and not the widget itself. Therefore adding this one line of code will override the title bar to not be embedded in the scene when it's parent was already embedded in the scene. Effectively making it reference the widget again and not the viewport.
In the MainWindow.cpp right after line 26 add
blue->setWindowFlags(Qt::BypassGraphicsProxyWidget);

Segmentation Fault while dynamicaly adding widget in Qt

I was trying to add a QWidget while runtime in Qt but It is showing SIGSEV signal received from OS because of segmentation fault.
Here is my code:
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QtGui>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_submit_clicked();
private:
Ui::MainWindow *ui;
QLabel *label;
QLineEdit *line_edit;
};
#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);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
if(str=="rana"&&str1=="vivek")
{
label = new QLabel();
label->setText("Success");
MainWindow.layout->addWidget(label);
label->show();
}
else
{
line_edit = new QLineEdit();
line_edit->setText("Sorry");
MainWindow.layout->addWidget(line_edit);
line_edit->show();
}
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
I know that segmentation fault occurs due to dereferencing of a null pointer but i couldn't find where I have done that mistake.Any Suggestions?
MainWindow.layout->addWidget(label);
doesn't make a lot of sense - this should not even compile, as Sebastian noted.
First, make sure you have layout in the Ui file (I added one vertical layout named verticalLayout), so you have a layout where you will add widgets. You will have a pointer to it inside your ui object.
Now, just use addWidget on that layout and everything should work:
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
if(str=="rana"&&str1=="vivek")
{
QLabel *label = new QLabel();
label->setText("Success");
ui->verticalLayout->addWidget(label);
// label->show(); widgets will became the part of the MainWindow, as the addWidget
// will add them into the hierarchy.
}
else
{
QLineEdit *line_edit = new QLineEdit();
line_edit->setText("Sorry");
ui->verticalLayout->addWidget(line_edit);
// line_edit->show()
}
}
Note - addWidget will set the owner of the widget to be the layout, so the widget will be deleted on the destruction of the layout.
Maybe implementing in this way will make sense?
void MainWindow::on_pushButton_submit_clicked()
{
QString str = ui->lineEdit1->text();
QString str1 =ui->lineEdit2->text();
QWidget *w = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout; // creates a vertical layout
if(str=="rana"&&str1=="vivek")
{
label = new QLabel(w);
label->setText("Success");
layout->addWidget(label);
}
else
{
line_edit = new QLineEdit(w);
line_edit->setText("Sorry");
layout->addWidget(line_edit);
}
w->setLayout(layout);
setCentralWidget(w);
}
UPDATE:
QMainWindow already has a predefined layout, so it was needless to introduce a new one. The code above creates an intermediate widget and construct it using its own layout. Than the widget set as a central widget in the MainWindow.

Letting QWidget and QVBoxLayout automatically resize if child resizes (Qt4)

I have the following minimal example code given.
main.cpp:
#include <QApplication>
#include "qt.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MyDialog mainWin;
mainWin.show();
return app.exec();
}
qt.cpp:
#include <QLabel>
#include "qt.h"
void MyDialog::setupUi()
{
setCentralWidget(new QWidget);
mainLayout = new QVBoxLayout( centralWidget() );
centralWidget()->setLayout(mainLayout);
// show the add new effect channel button
QPushButton* newKnobBtn = new QPushButton("new", this );
connect( newKnobBtn, SIGNAL(clicked()), this, SLOT(addNewKnob()));
mainLayout->addWidget( newKnobBtn, 0, Qt::AlignRight );
containerWidget = new QWidget(this);
scrollArea = new QScrollArea(containerWidget);
mainLayout->addWidget(containerWidget);
scrollLayout = new QVBoxLayout(scrollArea);
scrollArea->setLayout(scrollLayout);
/*
QSizePolicy pol;
pol.setVerticalPolicy(QSizePolicy::Expanding);
setSizePolicy(pol);
*/
addNewKnob(); // to fit size initially
}
void MyDialog::addNewKnob()
{
scrollLayout->addWidget(new QLabel("Hello World", this));
/*
containerWidget->adjustSize();
adjustSize();
*/
}
qt.h:
#include <QMainWindow>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QPushButton>
class MyDialog : public QMainWindow
{
Q_OBJECT
private slots:
void addNewKnob();
private:
void setupUi();
QVBoxLayout* mainLayout;
QScrollArea* scrollArea;
QVBoxLayout* scrollLayout;
QWidget* containerWidget;
public:
MyDialog( ) { setupUi(); }
};
Compiling: Put all in one directory, type
qmake -project && qmake && make
I have the adjustSize() solution from here, but it does not work: (link). Everything I commented out was things I tried but did not help.
How do I make containerWidget and scrollLayout grow correctly, when a new Label is being added to scrollLayout?
Here's a simplified version that works for me:
qt.cpp:
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include "qt.h"
MyDialog::MyDialog()
{
QWidget * mainWidget = new QWidget;
QBoxLayout * mainLayout = new QVBoxLayout(mainWidget);
setCentralWidget(mainWidget);
// show the add new effect channel button
QPushButton* newKnobBtn = new QPushButton("new");
connect( newKnobBtn, SIGNAL(clicked()), this, SLOT(addNewKnob()));
mainLayout->addWidget( newKnobBtn, 0, Qt::AlignRight );
QScrollArea * scrollArea = new QScrollArea;
scrollArea->setWidgetResizable(true);
mainLayout->addWidget(scrollArea);
QWidget * labelsWidget = new QWidget;
labelsLayout = new QVBoxLayout(labelsWidget);
scrollArea->setWidget(labelsWidget);
addNewKnob(); // to fit size initially
}
void MyDialog::addNewKnob()
{
labelsLayout->addWidget(new QLabel("Hello World"));
}
qt.h:
#include <QMainWindow>
#include <QBoxLayout>
class MyDialog : public QMainWindow
{
Q_OBJECT
public:
MyDialog( );
private slots:
void addNewKnob();
private:
QBoxLayout * labelsLayout;
};
You have containerWidget that contain only one QScrollArea. I don't know why do you need this. But if you need this for some reason, you need to add a layout to this widget in order to make layouts work. Also do not create a layout for QScrollArea. It already have internally implemented layout. You should add scrollLayout to the scroll area's viewport() widget instead.
When you construct a layout and pass a widget to its constructor, the layout is automatically assigned to the passed widget. You should not call setLayout after that. This action will take no effect and produce console warning.

Why am I not getting a display in my Qt Widget?

When I run it it will just finish right away and not show anything. I can't find anything wrong w/it and no one on #qt could either. I've got other apps working fine so I'm not sure. It's got something to do with the createForm call, if I omit that call in the constructor I do get a default QWidget displayed.
captchit.pro
#-------------------------------------------------
#
# Project created by QtCreator 2011-02-26T20:58:23
#
#-------------------------------------------------
QT += core gui network
TARGET = captchit
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
main.cpp
#include "QtGui/QApplication"
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget mainWidget;
mainWidget.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QPixmap;
class QLabel;
class QLineEdit;
class QPushButton;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
private slots:
void on_refreshButton_pressed();
void on_submitButton_pressed();
void on_closeButton_pressed();
private:
QPixmap captchaImage;
QLabel *imageLabel;
QLabel *statusLabel;
QLineEdit *captchaLineEdit;
QPushButton *submitButton;
QPushButton *refreshButton;
QPushButton *closeButton;
void createForm();
void createActions();
void getCaptcha();
void submitCaptcha();
};
endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "QtNetwork/QtNetwork"
#include "QtGui"
void Widget::on_refreshButton_pressed()
{
;
}
void Widget::on_submitButton_pressed()
{
;
}
void Widget::on_closeButton_pressed()
{
;
}
// Create UI Components
void Widget::createForm()
{
// Create Main Layout
QVBoxLayout *mainLayout = new QVBoxLayout(this);
// // set captcha pixmap to imageLabel for displaying
// imageLabel->setPixmap(captchaImage);
// Create Buttons
QVBoxLayout *buttonLayout = new QVBoxLayout();
submitButton->setText("Submit");
refreshButton->setText("Refresh");
closeButton->setText("Close");
buttonLayout->addWidget(submitButton);
buttonLayout->addWidget(refreshButton);
buttonLayout->addWidget(closeButton);
// Complete Layouts
// lineEdit & submitStatus
QVBoxLayout *lineEditLayout = new QVBoxLayout();
lineEditLayout->addStretch();
lineEditLayout->addWidget(statusLabel);
lineEditLayout->addWidget(captchaLineEdit);
lineEditLayout->addStretch();
// Create Bottom Layout
QHBoxLayout *bottomLayout = new QHBoxLayout();
bottomLayout->addLayout(lineEditLayout);
bottomLayout->addLayout(buttonLayout);
// Add to mainLayout
// mainLayout->addWidget(imageLabel);
mainLayout->addLayout(bottomLayout);
setLayout(mainLayout);
}
// Bind Slots and Signals
void Widget::createActions()
{
;
}
void Widget::getCaptcha()
{
;
}
void Widget::submitCaptcha()
{
;
}
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
createForm();
// createActions();
}
You need to initialize your private class members, probably in your constructor, before you use them.
Widget::Widget(QWidget* parent) :
QWidget(parent)
{
captchaImage = new QPixmap;
imageLabel = new QLabel(this);
statusLabel = new QLabel(this);
captchaLineEdit = new QLineEdit(this);
submitButton = new QPushButton(this);
refreshButton = new QPushButton(this);
closeButton = new QPushButton(this);
createForm();
// createActions();
}
Also note that QPixmap does not derive from QObject, so you'll have to delete it manually. It may be better to remove the QPixmap *captchaImage member from your class and use temporary QPixmap objects in your code.
Oops, it was because I forgot to initialize all of my components, herp derp.