How do I create a "hole" in my Qt window? - c++

I would like to create a "hole" in my QMainWindow.
Suppose the black rectangle is the "hole" of a QMainWindow. Inside the rectangle should be anything behind the QMainWindow. For example, if the QMainWindow is opened on top of my desktop, then inside the rectangle should be part of my desktop background image.
Is there any solution to achieve this?

You should use QWidget::setMask or QWindow::setMask. There is a Qt Example for that: https://doc.qt.io/qt-5/qtwidgets-widgets-shapedclock-example.html

Related

How to draw a caret/arrow bottom of a QDialog on qt5

I want to draw a caret / arrow top or bottom of a qt window. I cannot find any document regarding to this.
How can I accomplish this task with qt5? I've searched all possible words but can't find anything.
Can this be applied to QDialog or qml needed? My first choice is QDialog since I have a webengine and other qwidgets already in a QDialog.
I'm using C++.
Here is what I mean:
Most window-managers don't support non-rectangular windows directly, which means that if you want to do something like this you'll need to fake it by making the window large enough to include both its normal content and the desired caret-shape inside the window-area, and making the window transparent at the top.
To do that, call setAttribute(Qt::WA_TranslucentBackground) and setAttribute(Qt::WA_FramelessWindowHint) on your dialog, and override paintEvent(QPaintEvent *) to paint the dialog's background only for the parts of the dialog you want to be non-transparent.

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!

Scrollable background with resizable Qt widgets on it

I would like to create a Qt window that
can be scrolled/panned by dragging its background (scrollbars should not be shown)
contains multiple sub-widgets at defined locations (which scroll with the background)
the sub-widgets can be resized by pulling their border
What I managed so far is to create a QGraphicsView with setDragMode(QGraphicsView::ScrollHandDrag) http://doc.qt.io/qt-5/qgraphicsview.html#dragMode-prop. I then placed the sub-widgets on a QGraphicsScene, however, this didn't allow the sub-widgets to be resized by pulling their border.
I also tried to inherit my custom sub-widget class from QDialog, which allows setSizeGripEnabled(true). However, this doesn't resize their content, and QDialog is probably not meant to be part of a QGraphicsView.
Any suggestions? It would also be ok if the sub-widgets behave like sub-windows that can also be dragged at their title bar, as long as they cannot be closed and they move when the background is dragged.
You can look aside QMdiArea class (Qt documentation: QMdiArea). By problem description it is what you need.
Of course, you can use Graphics View Framework, but, i think, it will be more difficult. If you choose such approach, very useful will be class QGraphicsWidget (Qt documentation: QGraphicsWidget).

Best way to draw simple rectangle in Qt

I am writing a little program in C++ with Qt.
I have a QGridLayout with 3*3 QWidget. In each QWidget, I have a QVBoxLayout.
Within that QVBoxLayout I need to put a certain number of black and white rectangles.
For now, I use QWidgets for these rectangle and I apply a background-color to get the white and the black ones.
I saw in the documentation something about a Rectangle class that is linked with QtQuick and I don't really want to go into that.
Thanks for your answers,
I wouldn't bother with the layout and widgets unless you actually need interactive objects for each square. Just overload the paintEvent member of the widget you are using that contains the grid layout and use the QPainter object and call fillRect.
To get an outline around a widget, use a QFrame. Also QLabel subclasses QFrame. There are a lot of examples of using QFrame in the documentation.
Hope that helps.

Qt designer and draw rectangle on screen

Please see the following picture:
In above picture, i specefied a rectangle, i need to draw same rectangle in my program with Qt-Designer, But i don't know to use what widget.
Can i use qt designer or i should just use code?
Notes: I attemp to use QLabel and put --- and | but they don't stick.
My question is , Which widget can help me?
The box that you have circled in the image is a QGroupBox, and can be created using the QtDesigner. Other widgets can then be inserted into like any other widget that supports layouts.