Results 1 to 5 of 5

Thread: Auto adjust width of widget

  1. #1
    Join Date
    Sep 2011
    Location
    Netherlands
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Question Auto adjust width of widget

    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.

    Qt Code:
    1. void Gui::createDockWindows()
    2. {
    3. QDockWidget *dock = new QDockWidget(tr("File"), this);
    4. dock->setAllowedAreas(
    5. Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea |
    6. Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    7. dock->setWidget(editWindow);
    8. addDockWidget(Qt::RightDockWidgetArea, dock);
    9. viewMenu->addAction(dock->toggleViewAction());
    10. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Auto adjust width of widget

    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.

  3. #3
    Join Date
    Sep 2011
    Location
    Netherlands
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Auto adjust width of widget

    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?

  4. #4
    Join Date
    Sep 2011
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Auto adjust width of widget

    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.

  5. #5
    Join Date
    Sep 2011
    Location
    Netherlands
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Auto adjust width of widget

    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?

Similar Threads

  1. Replies: 8
    Last Post: 22nd March 2016, 04:39
  2. Replies: 2
    Last Post: 8th February 2011, 11:49
  3. QLabel auto width?
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 14th April 2009, 07:25
  4. How to adjust the a QHeaderView's width
    By culgi in forum Qt Programming
    Replies: 2
    Last Post: 31st January 2008, 08:35
  5. Replies: 1
    Last Post: 19th September 2007, 10:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.