PDA

View Full Version : How to let the close icon of QWidget on the right-up corner appear?



weixj2003ld
16th April 2012, 11:08
I create a widget and add it to a mainwindow,and find that the widget can not be closed,for it has not close icon on its right-up corner appear,how to do?

amleto
16th April 2012, 11:20
only windows have close buttons. widgets with parents are not windows.

weixj2003ld
16th April 2012, 11:51
In the constructor function create a widget and put a QTextEdit on it,show as the following code. Now I want to close the widget by click the close icon on its right-up corner and find there is nothing on it. How to do?


7591



MainWindow()
{
QWidget *widget=new QWidget;
QTextEdit *text=new QTextEdit;
QHBoxLayout *layout=new QHBoxLayout;
layout->addWidget(text);
widget->setLayout(layout);

setCentralWidget(widget);
}

wysota
16th April 2012, 12:06
I can see a nice big red cross in the upper right corner of the widget, can't you?

weixj2003ld
16th April 2012, 12:09
I can see a nice big red cross in the upper right corner of the widget, can't you?

it is that of mainwindow,and not that of the widget.

wysota
16th April 2012, 12:24
Why would you expect each widget to have its own close button? Have you ever seen anything like that? Only dock windows and mdi area subwidgets can be closed separately from the window that owns them since they are considered windows themselves.

nish
16th April 2012, 12:30
try QMainWindow/QMdiArea and friends.

weixj2003ld
17th April 2012, 04:45
Ok,I explain my question in detail.


Why would you expect each widget to have its own close button?

I want to do the following things:
1.For example,I have 9 child windows,and they arranged in 3 rows 3 lines,show as picture.(Maybe I have more than many child windows)
7593
2.I want to close each child window respectively.
3.If I double click one of them,it is floating.(that is,it can be out of the mainwindow)
4. I want them to zoom in or zoom out together.

So,QMainWindow can not satisfy my need, can it?.

I use for QSplitter, the oration of one is vertical,those of others are horizonal.
code:




//create widget1,and put a text1 on it,
QSplitter *horizonSpliter1=new QSplitter;
QWidget *widget1=new QWidget;

QTextEdit *text1=new QTextEdit;
QHBoxLayout *layout1=new QHBoxLayout;
layout1->addWidget(text1);
widget1->setLayout(layout1);

//create widget2,and put a text2 on it,
QWidget *widget2=new QWidget;
QTextEdit *text2=new QTextEdit;
QHBoxLayout *layout2=new QHBoxLayout;
layout2->addWidget(text2);
widget2->setLayout(layout2);


//create widget3,and put a text3 on it,
QWidget *widget3=new QWidget;
QTextEdit *text3=new QTextEdit;
QHBoxLayout *layout3=new QHBoxLayout;
layout3->addWidget(text3);
widget3->setLayout(layout3);

// insert widget1,widget2,widget3 into horizonSpliter1
horizonSpliter1->insertWidget(0,widget1);
horizonSpliter1->insertWidget(1,widget2);
horizonSpliter1->insertWidget(2,widget3);


//Does thing about horizonSpliter2 is the same as that of horizonSpliter1.
QSplitter *horizonSpliter2=new QSplitter;
QWidget *widget4=new QWidget;
QTextEdit *text4=new QTextEdit;
QHBoxLayout *layout4=new QHBoxLayout;
layout4->addWidget(text4);
widget4->setLayout(layout4);

QWidget *widget5=new QWidget;
QTextEdit *text5=new QTextEdit;
QHBoxLayout *layout5=new QHBoxLayout;
layout5->addWidget(text5);
widget5->setLayout(layout5);

QWidget *widget6=new QWidget;
QTextEdit *text6=new QTextEdit;
QHBoxLayout *layout6=new QHBoxLayout;
layout6->addWidget(text6);
widget6->setLayout(layout6);

horizonSpliter2->insertWidget(0,widget4);
horizonSpliter2->insertWidget(1,widget5);
horizonSpliter2->insertWidget(2,widget6);




//Does thing about horizonSpliter3 is the same as that of horizonSpliter1.
QSplitter *horizonSpliter3=new QSplitter;
QWidget *widget7=new QWidget;

QTextEdit *text7=new QTextEdit;
QHBoxLayout *layout7=new QHBoxLayout;
layout7->addWidget(text7);
widget7->setLayout(layout7);

QWidget *widget8=new QWidget;
QTextEdit *text8=new QTextEdit;
QHBoxLayout *layout8=new QHBoxLayout;
layout8->addWidget(text8);
widget8->setLayout(layout8);


QWidget *widget9=new QWidget;
QTextEdit *text9=new QTextEdit;
QHBoxLayout *layout9=new QHBoxLayout;
layout9->addWidget(text9);
widget9->setLayout(layout9);

horizonSpliter3->insertWidget(0,widget7);
horizonSpliter3->insertWidget(1,widget8);
horizonSpliter3->insertWidget(2,widget9);



QSplitter *verticalSpliter=new QSplitter;
verticalSpliter->setOrientation(Qt::Vertical);

//insert horizonSpliter1,horizonSpliter2,horizonSpliter3 into verticalSpliter.
verticalSpliter->insertWidget(0,horizonSpliter1);
verticalSpliter->insertWidget(1,horizonSpliter2);
verticalSpliter->insertWidget(2,horizonSpliter3);
....

wysota
17th April 2012, 09:12
See QDockWidget.

Spitfire
17th April 2012, 16:49
IMHO it's an odd approach but you can always try Qt::Tool (http://qt-project.org/doc/qt-5.0/qt.html#WindowType-enum) flag.

weixj2003ld
18th April 2012, 02:38
Ok.I will use QDockWidget.
Can it arrange all dockwidgets like the following pictures?
7600 7601

wysota
18th April 2012, 09:03
You won't know unless you try.

weixj2003ld
18th April 2012, 09:31
I have try,and it can not.Could you tell me how to do?

wysota
18th April 2012, 10:22
What have you tried?

weixj2003ld
18th April 2012, 11:39
I paste part of my test code


//dock1
QDockWidget *dock = new QDockWidget(tr("dock1"), this);
dock->setAllowedAreas(Qt::AllDockWidgetAreas);
customerList = new QListWidget(dock);
customerList->addItems(QStringList()
<< "dock1"
dock->setWidget(customerList);
addDockWidget(Qt::AllDockWidgetAreas, dock);

//dock2
dock = new QDockWidget(tr("dock2"), this);
dock->setAllowedAreas(Qt::AllDockWidgetAreas);
paragraphsList = new QListWidget(dock);
paragraphsList->addItems(QStringList()
<< "dock2."
dock->setWidget(paragraphsList);
addDockWidget(Qt::AllDockWidgetAreas, dock);

//dock3
dock = new QDockWidget(tr("dock3"), this);
dock->setAllowedAreas(Qt::AllDockWidgetAreas);
paragraphsList = new QListWidget(dock);
paragraphsList->addItems(QStringList()
<< "dock3."
dock->setWidget(paragraphsList);
addDockWidget(Qt::AllDockWidgetAreas, dock);


I create three dockwidgets,and setAllowedAreas() of them with the same value Qt::AllDockWidgetAreas(that is,they can be put any side of the mainwindow),and add them into the mainwindow with addDockWidget(Qt::AllDockWidgetAreas, dock),that is,dock them any side at first.

When the program run,I drag each of them to arrange them like the picture in the last post,but I can not however I drag them.

wysota
18th April 2012, 11:41
Did you set a central widget for your main window?

weixj2003ld
18th April 2012, 11:49
Thank you for your on-line answer.

no. Must be set it?
I want only the dockwidgets,and do not need the central widget.

weixj2003ld
19th April 2012, 03:54
I have set a central widget(created by QWidget) for the main window,and set its fixed size 1X1(because I do want it to be seen).

Now,I can arrange the three dock widget like the second picture in the #11 post.

But the following problem occur.

When I drag the dock widgets with mouse,I find that there is blank space between them,(I think, it may be the central widget,but I have set the size of it 1X1),could you tell me why?

7606

wysota
19th April 2012, 10:12
Because the dock on the left is attached to the left dock area and the docks on the right are attached to the right dock area. Try forcing all of them to be attached to the left area (by forbidding them from the right area).