Results 1 to 6 of 6

Thread: Add scrollbar to widget dynamically

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Add scrollbar to widget dynamically

    Dear All,

    have a widget (QWidgt, part of my mainwidow) . i'm creating textedit dynamically and adding to widget. I want to add a scrollbar into widget. Below is my code.

    Qt Code:
    1. #ifndef CCREATETEXTEDIT_H
    2. #define CCREATETEXTEDIT_H
    3.  
    4. #include <QWidget>
    5. #include <QHBoxLayout>
    6. #include <QVBoxLayout>
    7. #include <QTextEdit>
    8.  
    9. class CCreateTextEdit : public QWidget
    10. {
    11. public:
    12. CCreateTextEdit(QWidget* parent=0);
    13.  
    14. void AddTextEdit(QString str);
    15.  
    16. private:
    17. QHBoxLayout* buttonLay;
    18. QVBoxLayout* mainLay ;
    19. };
    20.  
    21. #endif // CCREATETEXTEDIT_H
    To copy to clipboard, switch view to plain text mode 



    //! .cpp file
    Qt Code:
    1. #include "ccreatetextedit.h"
    2.  
    3. CCreateTextEdit::CCreateTextEdit(QWidget* parent)
    4. : QWidget(parent)
    5. {
    6.  
    7. buttonLay = new QHBoxLayout;
    8. mainLay = new QVBoxLayout(this);
    9. mainLay->addLayout(buttonLay);
    10. }
    11.  
    12. void CCreateTextEdit::AddTextEdit(QString str)
    13. {
    14. QTextEdit *p = new QTextEdit(str);
    15. p->setFixedSize(100, 30);
    16. mainLay->addWidget(p);
    17. }
    To copy to clipboard, switch view to plain text mode 

    inside mainwindow ctor

    Qt Code:
    1. p = new CCreateTextEdit(ui->widget);
    2.  
    3. p->move(10, 10);
    4.  
    5. for(int i = 1; i <= 10; ++i)
    6. p->AddTextEdit(QString::number(i));
    To copy to clipboard, switch view to plain text mode 

    where p is a CCreateTextEdit pointer;

    plase help me.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add scrollbar to widget dynamically

    QTextEdit inherits QAbstractScrollArea. The member function of your interest is QAbstractScrollArea::setHorizontalScrollBarPolicy and QAbstractScrollArea::setVerticalScrollBarPolicy . In your case the policy will be Qt::ScrollBarAsNeeded

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Add scrollbar to widget dynamically

    I think that what OP wants is a scrollable widget that contains dynamically created text edits. Thus my suggestion is to wrap CCreateTextEdit into a QScrollArea using QScrollArea::setWidget(). Remember to set the widgetResizable property of the scroll area to true.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add scrollbar to widget dynamically

    Quote Originally Posted by wysota View Post
    I think that what OP wants is a scrollable widget that contains dynamically created text edits. Thus my suggestion is to wrap CCreateTextEdit into a QScrollArea using QScrollArea::setWidget(). Remember to set the widgetResizable property of the scroll area to true.
    Hi,

    i ve to dynamically created On click a tool button textedit and have to add in a widget, part of mainwindow.

    widget size is fixed to 100 x100.
    and scrollbar should be display automatically if the added to widget.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  5. #5
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add scrollbar to widget dynamically

    Rather than dynamically creating, why not use QWidget::setVisible or QWidget::setEnabled. It will do the same.

  6. #6
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add scrollbar to widget dynamically

    Quote Originally Posted by pkj View Post
    Rather than dynamically creating, why not use QWidget::setVisible or QWidget::setEnabled. It will do the same.
    No it didnot work. Actually in mainwidow i've dragged and added a widget by using creator. Now I've a class(CMyTextEdit) which will create a textedit.
    In mainwindow i've a push button. On clicking a push button, i'm creating pointer of the class (CMyTextEdit *) which create the text edit, and for CMyTextEdit ui->widget is the parent. I want to add a scrollbar to ui->widget if the item is increaing.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

Similar Threads

  1. create a new widget dynamically
    By rimie23 in forum Qt Programming
    Replies: 18
    Last Post: 24th May 2012, 14:29
  2. Replies: 8
    Last Post: 2nd February 2012, 19:52
  3. Replies: 5
    Last Post: 14th December 2011, 11:51
  4. access the widget dynamically
    By mohanakrishnan in forum Qt Programming
    Replies: 4
    Last Post: 26th November 2009, 12:37
  5. Replies: 1
    Last Post: 25th February 2009, 05:35

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
  •  
Qt is a trademark of The Qt Company.