QDialog as top-level window doesn't work - qt

I have a main QWindow and a QFrame (mainF) on it, which contains some other gui elements. I maximize the QFrame as follows, if I click on a button on my QWindow:
mainF->setWindowFlags(Qt::Dialog);
mainF->setWindowState((mainF->windowState(), Qt::WindowFullScreen));
mainF->show();
To minimize the QFrame and place it in its previous position again, I have created a QDialog (m_MinimizeFullscreenGui) with a button on it, which appears immediately on the right top corner of my QFrame if I maximize it:
QRect windowRectangle= m_MinimizeFullscreenGui->geometry();
WindowRectangle.moveTopRight(QApplication::desktop()->availableGeometry().topRight());
m_MinimizeFullscreenGui->setGeometry(windowRectangle);
m_MinimizeFullscreenGui->setFixedSize(30, 30);
m_MinimizeFullscreenGui->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
m_MinimizeFullscreenGui->setWindowState(m_MinimizeFullscreenGui->windowState());
m_MinimizeFullscreenGui->show();
The problem is: I want to have my small dialog window always on the top-level of my maximized QFrame. But if I click at somewhere on my QFrame, the dialog window goes in the background, so the Qt::WindowStaysOnTopHint flag doesn't work.
Where can be my mistake?

Related

How to dock a QDockWidgets inside a QSplitter?

I am trying to have a QSplitter accept QDockWidgets in my application. So far, I have done everything through the Qt Designer and what I have done is create three individual QWidgets. I then select all three of the QWidgets and I right click on them and select Layout->Lay out Vertically in a Splitter.
This lays all three of the widgets in a splitter quite nicely. I then drag a Dock Widget to the Object/Class Window in the top right and set them in the Splitters Widget. This places the QDockWidget happily inside the widget. However, when I fire up the program I cannot click and drag the dock widgets. If I double click the dock widget, the dock widget will pop out, however I cannot place it back since it was never technically docked. Which then creates the problem of the widget not being allowed to dock anywhere else. It cannot be docked on the QMainWindow class or in the QSplitter class.
Is there anyway to have a QDockWidget docked inside of a QSplitter and have the functionality of a QDockWidget?
After you add the dock widget to the QSplitter, the widget has become part of the splitter.
You can try checking like this
//If sure of Dockwidget at zeroth position
QDockWidget *widget1 = (QDockWidget*)ui->splitter->children().at(0);
A Dockwidget has a feature of floating as a top level window.
But you can make a dockwidget look like other widgets by setting QDockWidget::NoDockWidgetFeatures
Either:
Go to the object window in Qtdesigner (top -> right)
And select the dock widget added to splitter.
In proeprties window, down below scroll down and look for "features".
Then uncheck the features like movalble, closable etc....
I made it NoDockWidgetFeatures.
or
You can set programmatically using setFeatures(QDockWidget::NoDockWidgetFeatures)

Prevent QLabel from resizing parent Widget

I have a QLabel inside a QFrame.
Sometimes I have too much text in the QLabel and it resizes the QFrame where it is in.
Now, I want to prevent the QLabel from resizing the QFrame where it resides in.
I don't want to limit the amount of lines or setting the maximum size of the QLabel because if the window size of the app increases, I do want to allow the QLabel to increase in size.
Just want to prevent the QLabel from expanding it's parent.
Any clean way to do it?
Use a QScrollArea (which inherits QFrame), and hide its scrollbars:
label = QtGui.QLabel(text)
frame = QtGui.QScrollArea()
frame.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
frame.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
frame.setWidgetResizable(True)
frame.setWidget(label)
This has the side-benefit that the user will still be able to view any hidden text by scrolling with the mouse-wheel.

Qt - draw inside QScrollArea in a QDialog

In Qt5, I have a QDialog window on which I have drawn a circle as follows:
void MyDialog::paintEvent(QPaintEvent *pe)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
QPen pen(Qt::blue,2);
painter.setPen(pen);
QRect r=QRect(0,0,100,100);
painter.drawEllipse(r);
}
If I draw a larger circle, for example by using QRect(0,0,500,500);, the circle being greater than the dialog window is clipped. So I dragged a QScrollArea onto the the dialog window and decide to draw onto that so that scroll bars are automatically added. The QScrollArea can be accessed using ui->scrollArea.
I changed the above code by setting QPainter painter(ui->scrollArea);. However, nothings appears in the QScrollArea. I read that I need to override the paintEvent of QScrollArea. But I don't know how to do this.
Any help of drawing on the QScrollArea?
Drawing on the QScrollArea is not what you want either because the QScrollArea actually has a viewport widget.
Create another class which inherits QWidget. Override the paintEvent() method and to the painting you mention. Then, add the widget to the scroll area in your dialog.
MyDialog::MyDialog()
{
QScrollArea *pScrl = new QScrollArea(this);
pScrl->setWidget(new MyWidget(pScrl));
... // use a layout to put the scroll area in the dialog
}
To really make it useful you will need to resize the MyWidget instance to the size of the circle that you want to draw.

QDockWidget is not docked on a QDialog

Is it correct to use a QDockWidget on a QDialog? When I tried to use it, dock widget did not get docked on the dialog window. I could not resize the dock widget when I executed the application.
QDockWidgets have to be 'owned' by a QMainWindow, but you can of course put a QMainWindow inside a QDialog.

How to remove the window border (containing minimize, maximize and close buttons) from a Qt widget?

I would like to animate a widget (QPushButon) to move across my application screen. For that I create a new button and using the QPropertyAnimation class and the property "geometry" of the button, I move it from top to down. The problem is that the button comes with the close, minimize, maximize buttons, etc. I don't want them to be there, nor the border that comes with the widget. What should I do ?
You want to use the function QWidget::setWindowFlags( Qt::WindowFlags ).
If you want to remove the maximize/minimize/close buttons, this should work for you:
setWindowFlags( Qt::CustomizeWindowHint );
Qt::CustomizeWindowHint turns off all the default window hints, like the maximize, minimize, close buttons, and the title bar.
Here's a list of all Qt::WindowFlags.

Resources