PDA

View Full Version : How to make frameless widget as a window or dialog



ainne810329
3rd November 2011, 08:59
I made a frameless widget which has its own titlebar. When this widget has a parent widget, it doesn't act as a separate window anymore, but I want to have it popup as a dialog. Unfortunately I can't minimize, maximize it. It stays inside the parent widget frame. How can I make this frameless widget still act as an independent window or dialog when it has a parent widget?

The code looks something like this:

#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QMainWindow mainWin;
mainWin.show();

FramelessWidget myWid(&mainWin, Qt::Dialog | Qt::FramelessWindowHint);
myWid.show();

return app.exec();
}

Anyone has any ideas?

nudels
3rd November 2011, 10:05
Hi :)

ok first of all, what kind of widget is your FramelessWidget? QWidget? QDialog? QObject?
when its from QDialog, then u dont need to set a parent in your constructor:


main.cpp:
...
FramelessWidget myWid(Qt::Dialog | Qt::FramelessWindowHint);
myWid.show();

but then u have to change your constructor in your framelesswidget.cpp to:


FramelessWidget(..., QWidget* parent = 0);

because arguments with default-values must be at last positions in the constructor.

if your FramelessWidget is inhertide from QWidegt, then u need a QDialog or QMainWindow like:



main.cpp:
{
...
QMainWindow mainWin;
mainWin.show();

QDialog dia;
FramelessWidget(&dia, Qt::Dialog | Qt::FramelessWindowHint);
dia.show();

return app.exec();
}


this code snippet creates one MainWindow and one Dialog-Window.

i hope i could help you :)

with best regards

nudels

ainne810329
3rd November 2011, 11:24
First, thank you very much for your respond.

The FramelessWidget is derived from QWidget indeed, by using a QDialog as a parent of my frameless widget, the purpose to have custome title bar and borderless dialog can not be achieved anymore.

Secondary, I do want this frameless widget to be a child dialog of the mainwindow, so it stays on top of the mainwindow and when the mainwindow is minimized, the frameless dialog is also minimized.

Any idea how I can achieve my goals?

nudels
3rd November 2011, 11:38
can u plz post the framelesswidget constructor?

u want to have one Window, that is the parent of ur framelesswidget? is that right?

best regards

nudels

ainne810329
3rd November 2011, 12:51
What I really want is a frameless dialog which has a parent window and the frameless widget itself still can act as an independent window although it has a parent.

Here is the code of the frameless widget:

class FramelessWidget : public QWidget
{
FramelessWidget(QWidget *parent = 0, Qt::WindowFlags f = 0) : QWidget(parent, f)
{
QVBoxLayout *layout = new QVBoxLayout();
setLayout(layout);

TitleBar *titleBar = new TitleBar(this);
layout->addWidget(titleBar);
......
}
......
}

Added after 4 minutes:

Saying it in other words, the FramelessWidget can be created as window, dialog or whatever other widget type as a normal QWidget does. The only difference is the widget has custom titlebar and no window border even when it is a window or dialog.

It works fine as I want when it doesn't have a parent widget. But when there is a parent widget, this FramelessWidget can not act as an independent window or dialog anymore. It acts more like frame inside the parent widget.

firejacky
4th November 2011, 07:18
You can combine window flags Qt::Tool and Qt::FramelessWindowHint to achieve this effect.

ainne810329
4th November 2011, 10:27
combine Qt:Tool and Qt::FramelessWindowHint doesn't work as I expected. It works exactly the same as combining Qt::Dialog and Qt::FramelessWindowHint.

the frameless window which has a parent can't be minimized or maximized as a normal window, it stays inside the central widget of the parent window.

Added after 8 minutes:

Sorry!!!

I try to set the frameless window with no parent, then combination of Qt::Tool and Qt::FramelessWindowHint works. But it has other problems. Now this frameless window has an entry on the task bar, it can't minimized. Normally, a child dialog won't have entry on the task bar.

Any ideas to solve the task bar problem???