Qt QGraphicsView not showing the scene - qt

I am new to Qt. I got a problem. I do not want to use Qt forms to create QGprahics view I just want to code it and I can't get it work.
here is my code:
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.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
scene = new QGraphicsScene(this);
scene->setSceneRect(600,480,600,480);
QGraphicsEllipseItem * ellipse;
QPen pen(Qt::red);
QBrush brush(Qt::blue);
ellipse = scene->addEllipse(150,150,100,100,pen, brush);
view = new QGraphicsView(scene,this);
setCentralWidget(view);
}
MainWindow::~MainWindow()
{
delete view;
delete scene;
}
view and scene are privete members of class MainWindow.
Program just shows white screen but there is no ellipse there.
any suggestions what am I doing wrong? Thanks in advance!

I think that you problem is scene->setSceneRect(600,480,600,480);.
Your ellipse is created outside of the view.

Related

Can we include and show some others widgets in a QVideoWidget? [duplicate]

everyone! I try to set a click property to a QMediaPlayer Element, but I can not find the mode to make it, and if I try to put a button in front to Video, the button puts behind to video, even with
button->raise();
videoWidget->lower();
And If I put a Button to fullscreen the screen turns in black and don't shows the video
this id the code of the video player
QMediaPlayer *player = new QMediaPlayer(this);
QVideoWidget *vw = new QVideoWidget(this);
QMediaPlaylist *PlayList = new QMediaPlaylist(this);
PlayList->addMedia(QUrl::fromLocalFile("/home/user/Videos/video.mp4"));
PlayList->setPlaybackMode(QMediaPlaylist::Loop);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(vw);
player->setVideoOutput(vw);
player->setPlaylist(PlayList);
vw->setGeometry(0,0,800,480);
vw->show();
player->play();
One possible solution is to create a widget where the QVideoWidget is placed through a layout, the button is also added and we change the position through the resizeEvent() event.
#include <QApplication>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QPushButton>
#include <QUrl>
#include <QVBoxLayout>
#include <QVideoWidget>
#include <QDebug>
class VideoWidgetButton: public QWidget{
QPushButton *btn;
QVideoWidget *vw;
QMediaPlayer *player;
public:
VideoWidgetButton(QWidget *parent=Q_NULLPTR):QWidget(parent){
setLayout(new QVBoxLayout);
layout()->setContentsMargins(0, 0, 0, 0);
vw = new QVideoWidget(this);
btn = new QPushButton(this);
btn->setIcon(QIcon(":/icons/tux.jpeg"));
btn->resize(QSize(128, 128));
btn->setIconSize(QSize(128, 128));
connect(btn, &QPushButton::clicked, [](){
qDebug()<<"clicked";
});
layout()->addWidget(vw);
player = new QMediaPlayer(this);
player->setVideoOutput(vw);
QMediaPlaylist *playList = new QMediaPlaylist(this);
playList->addMedia(QUrl("qrc:/video/SampleVideo_1280x720_1mb.mp4"));
playList->setPlaybackMode(QMediaPlaylist::Loop);
player->setPlaylist(playList);
player->play();
}
protected:
void resizeEvent(QResizeEvent *ev){
btn->move(rect().bottomRight()-btn->rect().bottomRight());
return QWidget::resizeEvent(ev);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VideoWidgetButton w;
w.resize(640, 480);
w.show();
return a.exec();
}
The complete example can be found in the following link.

create a multiple Mainwindow in QT

Thank you for going through this post. I have searched forums,blogs and SO, but could not get what I actually need.
I am experimenting on how to display multiple mainwindows. I am using a embedded hardware board. I have successfully ported QT lib on to it.
I have written a small program.
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow text_plane;
text_plane.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
text_plane.setStyleSheet("background-color: Black;");
text_plane.show();
a.exec();
return a.exec();
}
The above code displays one window only.
Even if I create a Mainwindow w1 after text_plane.show() like
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow text_plane,w1;
text_plane.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
text_plane.setStyleSheet("background-color: Black;");
text_plane.show();
w1.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
w1.setStyleSheet("background-color: Yellow;");
w1.show();
a.exec();
return a.exec();
}
Now only the w1 window is shown. What about the text_plane window ?? how to get that back.
Can anyone help me out here to make this understand.
Thank you
First of all, I wonder, what are you trying to achieve here:
a.exec();
return a.exec();
You need to call this method only once, you know.
Second of all, both of your windows are shown (you can see it in a taskbar), but, since you set Qt::FramelessWindowHint, one of them is drawn on top of the other. You can split them by using move (or something like it):
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w, w1;
w.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
w.setStyleSheet("background-color: Black;");
w.show();
w1.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
w1.setStyleSheet("background-color: Yellow;");
w1.show();
w.move(0, 0);
w1.move(100, 100);
return app.exec();
}

Is it possible to implement SystemTrayIcon functionality in Qt Quick application

I am writing a QtQuick desktop application. I use both c++ (for functionality) and QML (for UI) in it.
I use QQuickView to show the interface written in QML.
I want this application to reside in System Tray when minimised.
I mean a functionality similar to this example. http://qt-project.org/doc/qt-4.8/desktop-systray.html .
I am trying to implement this feature but could not find a way to do this in my Qt Quick application.
Here is my main.cpp code:
#include <QGuiApplication>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlFileSelector>
#include <QQuickView>
#include "myapp.h"
int main(int argc, char* argv[])
{
QGuiApplication app(argc,argv);
app.setApplicationName(QFileInfo(app.applicationFilePath()).baseName());
QDir::setCurrent(qApp->applicationDirPath());
MyApp myappObject;
QQuickView view;
view.connect(view.engine(), SIGNAL(quit()), &app, SLOT(quit()));
view.rootContext()->setContextProperty("myappObject", &myappObject);
new QQmlFileSelector(view.engine(), &view);
view.setSource(QUrl("qrc:///myapp.qml"));
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.show();
return app.exec();
}
Please help by providing any hint/pointers to do this.
Thanks.
I was facing the same challenge today and ended up using the following solution within main(). Works great for me when using Qt 5.3. You should of course implement a better way to check whether the first root object is your application window object or not.
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QMessageBox>
#include <QAction>
#include <QMenu>
#include <QSystemTrayIcon>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
QMessageBox::critical(0, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray "
"on this system."));
return 1;
}
QApplication::setQuitOnLastWindowClosed(false);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
QObject *root = 0;
if (engine.rootObjects().size() > 0)
{
root = engine.rootObjects().at(0);
QAction *minimizeAction = new QAction(QObject::tr("Mi&nimize"), root);
root->connect(minimizeAction, SIGNAL(triggered()), root, SLOT(hide()));
QAction *maximizeAction = new QAction(QObject::tr("Ma&ximize"), root);
root->connect(maximizeAction, SIGNAL(triggered()), root, SLOT(showMaximized()));
QAction *restoreAction = new QAction(QObject::tr("&Restore"), root);
root->connect(restoreAction, SIGNAL(triggered()), root, SLOT(showNormal()));
QAction *quitAction = new QAction(QObject::tr("&Quit"), root);
root->connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
QMenu *trayIconMenu = new QMenu();
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
QSystemTrayIcon *trayIcon = new QSystemTrayIcon(root);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setIcon(QIcon(":/resources/DatagnanLogoColor.png"));
trayIcon->show();
}
return app.exec();
}
Copy the Windos class (window.cpp/window.h) from systray example to your project, port it to Qt5 if necessary and open both from your main file:
int main(int argc, char* argv[])
{
// ...
QQuickView view;
// ...
view.show();
Window window;
window.show();
return app.exec();
}

remove extra widget/window in Qt

probably a simple question: I just created a new project in Qt creator and I set it to use QWidget when I created it, now how do I get rid of the window that it automatically creates when I run it? I also created my own QWidget window which I want to be the only window.
#include "widget.h"
#include <QtGui>
Widget::Widget()
{
QWidget* window = new QWidget;
addBtn = new QPushButton("Add Module");
text = new QTextEdit();
text->setReadOnly(true);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(addBtn,5);
layout->addWidget(text);
window->setLayout(layout);
window->show();
}
Widget::~Widget()
{
}
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
change it like this
Widget::Widget()
{
addBtn = new QPushButton("Add Module");
text = new QTextEdit();
text->setReadOnly(true);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(addBtn,5);
layout->addWidget(text);
this->setLayout(layout);
}
and try to have some time on look and try some of Qt Example (you can find it in Qt Creator)
and there is about 100 short video to learn quickly basic stuff on Qt
Qt is fun, enjoy it.

Add QRadioButtons on runtime

How i can add QRadioButtons in a QFrame on runtime?
Thanks.
Add the widget to the appropriate place by calling the addWidget() method, such as:
ui->someLayout->addWidget(widgetToAdd);
Just make sure you do this in your main (UI) thread.
you can add a QRadioButtons on runtime normally in the same way you do before runtime.
you create the QRadioButton dynamically and call the addWidget method of QFrame layout.
if you are not ableto do it, post the code and let me show you.
mainwindow.h
#include <QtGui/QMainWindow>
#include <QPushButton>
#include <QHBoxLayout>
#include <QRadioButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
QHBoxLayout * layout;
QPushButton * button;
public:
MainWindow(QWidget *parent = 0);
public slots:
void radioAdd();
};
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
layout = new QHBoxLayout(this);
QWidget * w = new QWidget(this);
w->setLayout(layout);
this->setCentralWidget(w);
button = new QPushButton(QString("push"),this);
layout->addWidget(button);
connect(button,SIGNAL(clicked()), this, SLOT(radioAdd()));
}
void MainWindow::radioAdd() {
QRadioButton * radio = new QRadioButton("Search from the &cursor", this);
layout->addWidget(radio);
}
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
in this code the radioButton get created in the runtime (in the slot function radioAdd). and in your case, instead of adding QRadioButton into the wigdet layout you add them into QFrame.

Resources