PDA

View Full Version : Auto adjust width of widget



Sjaacko
21st September 2011, 07:58
Hi all,
In my program I have a widget where a text file is loaded and where you can edit it.
Various actions that the program does also adds (or removes) content from the file.
This is the code that creates it.



void Gui::createDockWindows()
{
QDockWidget *dock = new QDockWidget(tr("File"), this);
dock->setAllowedAreas(
Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea |
Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
dock->setWidget(editWindow);
addDockWidget(Qt::RightDockWidgetArea, dock);
viewMenu->addAction(dock->toggleViewAction());
}


The question is: how do I auto resize the width of the widget when content is changed to the max width of the longest line of text?
I found this can probably done with adjustSize() or QWidget::sizeHint() but I don't know where to put it.
Please help?
Thank you.

Sjaacko

goblincoding
21st September 2011, 19:20
This is purely hypothetical as I can't tell you how to solve your problem with any certainty, but I would have started by looking into connecting the QTextEdit::textChanged signal to a slot that would call QWidget::setContentsMargins with values obtained from your max width calculation.

Sjaacko
30th September 2011, 13:13
Sorry for late reply but thank you for answering. Unfortunately I have no idea where to put the code you sugested. Any more thoughts on how to implement this?

goblincoding
5th October 2011, 18:22
Hi Sjaacko

I see it's been 5 days since you replied, I have to apologise, but free time is a definite commodity at the moment.

If you haven't solved your problem yet, to answer your question, you can connect signals and slots practically anywhere where you have access to the objects you wish to connect. More specifically, I would have created a slot in whichever widget would ultimately be responsible for the creation of the dockwidget, done all my calculations there and played with the QWidget::setContentsMargins concept until I found something that worked. As to where you could connect the two (the QTextEdit signal and custom widget slot), the best place would probably be where you create the QTextEdit.

It's a bit difficult to give exact input as I don't have anything definitive in front of me (code wise) but I hope this makes sense to some degree.

Sjaacko
7th October 2011, 07:47
Thanks goblincoding,
It does make sense, but indeed to some degree. I still have no idea where to put what code.
Thanks anyway, I will continue my search to finding a solution.
Meanwhile when you have any more ideas, let me know?