Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a child widget standalone?

Tags:

c++

qt

Suppose I have a main Widget, which is used to view some objects. The names of those objects are stored in a QListWidget. Now when a user selects one object (one item of the QListWidget), I want to open another widget in a separate window which takes the name of the object as an argument.

class MainWidget
{
Q_OBJECT

public slots:
    void openSelection();

};

class ChildWidget
{
public:
    ChildWidget(QString name, QWidget* parent = nullptr);
};

void MainWidget::openSelection()
{
    QString selectedObjectName = ui->objectsNamesList->selectedItem()->text();
    ChildWidget* detaildedWiew = new ChildWidget(selectedObjectName, this);
    detaildedWiew->show();
}

When I do this, the child widget opens, but it has no space of its own. It is locked in the area of the parent. I need to set the children free, to run around the screen freely, independent of their parent. How can I do this? Is there some Qt way, or do I have to define some "pseudo child" relationship and develop a system to properly delete the pseudo children?

like image 866
Martin Drozdik Avatar asked Jul 27 '12 09:07

Martin Drozdik


2 Answers

You can use QWidget::setWindowFlags(Qt::Window) to make your widget a separate window. Also have a look at Qt::WindowFlags.

like image 145
hank Avatar answered Oct 22 '22 23:10

hank


Not sure I got the question right...

The default constructor of QWidget may accept two arguments:

  • QWidget *parent = 0 is the parent of the new widget. If it is 0 (the default), the new widget will be a window. If not, it will be a child of parent, and be constrained by parent's geometry (unless you specify Qt::Window as window flag).

  • Qt::WindowFlags f = 0 (where available) sets the window flags; the default is suitable for almost all widgets, but to get, for example, a window without a window system frame, you must use special flags.

So, if you pass anything but NULL to parent, your wiget will not be a separate window, unless you set the second parameter as QT::Window. This is what's happening for you. So you'll need to either set the flag QT::Window, or make your own class, derive it from QWidget, and write a constructor that takes an additional argument which will be the pointer you need, while getting NULL as parent.

like image 34
SingerOfTheFall Avatar answered Oct 22 '22 21:10

SingerOfTheFall