QGraphics scence - c++

I have trouble using the QgraphicsScene in Qt.
When I am initializing the QGraphicsScene in the main.cpp i am able to use that. But when I am try to add that to an Widget & then add that to the main window its not working, I am not able to view anything. What is the problem?

You should put the QGraphicsScene in a QGraphicsView. Then put the QGraphicsView in your widget.

Related

Qt Accidental activation of dragLeaveEvent,what should I do?

I have a QPushButton on a QScrollArea, the parent of the QPushButton is the QScrollArea. I drag the object to the QScrollArea and then to the QPushButton which always activates the dragLeaveEvent of the QScrollArea, I don't want this function to activate, what should I do?
Thank you for your replies, I have solved the problem.
The method is very simple, just set the parent of the QPushButton to QScrollArea::widget() and it's solved.

How disable window move inside an area?

I'm trying to write a circuit designer software in QT on Linux. I'm using KDE 5 Plasma desktop and QTCreator as an IDE.
I tried to use QFrame paintEvent to paint on it, and it worked, but when im grabbed the window inside QFrame it moved.
I know about QGraphicsView, but i cant make a custom class and promote it based on that(it's not listed).
How can i create a custom class from a container(QFrame, QGraphicsView or anything) where i can override paint event and also it doesn't move window if i grab it?
Sorry for my poor english.
QGraphicsView inherits from QAbstractScrollArea which inherits from QFrame itself.
So you can keep the QFrame in the form, and keep it promoted to your canvas class, but simply make you canvas class inherit QGraphicsView instead.
Although, my Qt has two differences in behavior from the OP (but I don't use KDE):
Clicking on a QFrame and moving the mouse doesn't move the whole window for me. I guess this behavior for the OP could be changed by reimplementing void mousePressEvent ( QMouseEvent * event ) in the canvas class and giving it an empty code instead. (doc)
I can put QGraphicsView in my ui files, and I can right click on them to promote them to another custom-defined class.
Edit: Found the reason why the window moves on KDE!

Promote QWidget to QMainWindow or add QMainWindow to QWidget from Qt Designer

My problem:
I want to customize the way the title bar works and looks for my application.
My idea:
I created a new QWidget form in Qt Designer and added a QWidget to it. I added the following code in constructor:
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint);
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(20);
effect->setXOffset(0);
effect->setYOffset(0);
setGraphicsEffect(effect);
which makes the outer widget transparent and adds shadow to my inner widget. From this on I can create a custom title bar widget which I can implement however I want.
This is the result:
My issue
I want to make this usable from the designer as a main window and the QWidget doesn't allow me to add FROM THE DESIGNER tool bars, menu bar and status bar.
What I thought about was adding a QMainWindow widget as a child widget for the outer QWidget(which is transparent and acts as support for my shadow(the shadow is drawn on it)). I did this successfully but only from code:
QMainWindow *centralwidget = new QMainWindow();
centralwidget->setStyleSheet("background-color: lightgray;");
centralwidget->setGeometry(0, 0, 50, 20);
centralwidget->setWindowFlags(Qt::Widget);
this->layout()->addWidget(centralwidget);
QMenuBar *menuBar = new QMenuBar(centralwidget);
menuBar->addAction("Action");
QStatusBar *statusBar = new QStatusBar;
statusBar->showMessage("Status bar here");
centralwidget->addToolBar("tool bar");
centralwidget->setMenuBar(menuBar);
centralwidget->setStatusBar(statusBar);
This is the result:
My question:
How can I achieve this result from Qt Designer? Is it possible to promote a QWidget to QMainWindow? I cannot think to another way of doing it... It is really important for me to make it usable from Qt Designer because I intend to make it a template widget and be able to create e.g. a new QCustomMainWindow form Qt Creator just like you can create a QWidget or a QMainWindow.
Please help!
Here is another SO question similar to yours: Qt4: Placing QMainWindow instance inside other QWidget/QMainWindow
Just adding on to my original comment:
Start with a QMainWindow, and then apply the appropriate flags to it. QMainWindow is a subclass of QWidget. If it can't be done easily in the designer, it is pretty painless to do in code. Do it in your constructor right after the ui->setup() call.
Start with QMainWindow
Customize Window Flags
So in the constructor in mainwindow.cpp, you put
http://qt-project.org/doc/qt-5/qt.html#WindowType-enum
this->setWindowFlags(Qt::Widget);
This is the default type for QWidget. Widgets of this type are child
widgets if they have a parent, and independent windows if they have no
parent. See also Qt::Window and Qt::SubWindow.
// or if you want to apply more than one you, "or" it together, like so:
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
Try out a couple of those and see what you like.
Customize Widget Attributes
There are also Widget Attributes, that give you strong control over how your widgets look like and behave.
http://qt-project.org/doc/qt-5/qt.html#WidgetAttribute-enum
Qt Style Sheets
In addition to all the flags and attributes above, you can also modify a ton of it with stylesheets:
http://qt-project.org/doc/qt-5/stylesheet-reference.html
this->setStyleSheet("background: #000000;");
Qt Designer Custom Widgets
And also if you are interested in making this a reusable thing in Qt Designer, you can make it into a Qt Designer plugin, or custom widgets.
http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html
http://qt-project.org/doc/qt-4.8/designer-creating-custom-widgets.html
QMdiArea and QMdiWindow
Another path to look into besides using QMainWindow is QMdiSubWindow
http://qt-project.org/doc/qt-5/QMdiSubWindow.html

Can't move QDockWidget

I want to create a widget that contains several QDockWidgets on purpose of putting it into a QMainWindow. Problem is that if I add QDockWidgets to my QWidget class with layout->addWidget(dockWidget);(I don't know any other way of doing it) and then setLayout(layout) I can't do anything to the QDockWidgets but dock and undock. I can't move them, I can't position them in another place.
QMainWindow has this feature addWidgets that QWidget doesn't have. Using QMainWindow everything works perfect, but I want it to work the same if I add a QWidget object(containing some QDockWidgets) to QMainWindow.
Is there any possibility to make my QWidget fully support those QDockWidgets, and use the on full potential(move, scale, dock, change position)?
Thanks
If you're using a lot of QDockWidgets, simply enabling dock nesting might be the solution to the underlying problem.
If you absolutely need to have a widget inside the QMainWindow, you can try putting another QMainWindow in the first one. You might have to set the windowFlags property of the second QMainWindow to Qt::Widget.

Qt add Widget to GraphicsView?

Is there a way to draw Widgets on QGraphicsView instead of QGraphicsScene so that the widget stays in position when the scene is moved?
I want to create some dialogs that are dockable inside the workspace like this:
http://www.thebandfrom.com/wp-content/uploads/photoshop-ui.png
You can use the addWidget function of QGraphicsScene, and then set the QGraphicsItem::ItemIgnoresTransformations flag to the added QGraphicsProxyWidget.
QGraphicsProxyWidget* proxyWidget = scene->addWidget(myWidget);
proxyWidget->setFlag(QGraphicsItem::ItemIgnoresTransformations);
You can add widgets onto the QGraphicsView directly by setting the QGraphicsView as their parent. You could also add a layout so that when the QGraphicsView is resized, your widgets arrange themselves appropriately.