Results 1 to 16 of 16

Thread: add items to treewidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: add items to treewidget

    First I thought that I would be able to simple to insert it into treeW with insertTopLevelItem function. In order of int index I tried to use dwNumberSpinbox->value() maybe it was good choise I don't know . because I don't how create treewidgetItem which has value of dwTypeCombo

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: add items to treewidget

    because I don't how create treewidgetItem which has value of dwTypeCombo
    Let me see if I understand:
    you want to create a QTreeWidgetItem() that holds the value of the current selected item in the combobox?
    Is that it?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: add items to treewidget

    something like that. while choosing swNumberSpinbox value and dwTypeCombo value to fully fill treewidget. Is it possible?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: add items to treewidget

    I am sorry, I just can't seem to understand you, and you seem unwilling to say what the problem is - which is strange if you want help.
    Maybe someone else understands what your problems is, I don't.

    Can't you say something along the lines of:
    "I have done XXXX, in order to achive YYYY and I have problems figuring ZZZZZ out." ?!
    Or anything that will explain the PROBLEM you have?

    Is it possible?
    "Everything" is possible.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: add items to treewidget

    May be QTreeWidget::insertTopLevelItem will help you

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

    Default Re: add items to treewidget

    Or QTreeWidget::topLevelItem and QTreeWidgetItem::setText

  7. #7
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: add items to treewidget

    Quote Originally Posted by high_flyer View Post
    Can't you say something along the lines of:
    "I have done XXXX, in order to achive YYYY and I have problems figuring ZZZZZ out." ?!
    Or anything that will explain the PROBLEM you have?
    I am making simple game editor. It's my studies project. Wich creates games objects. Now am trying to make planets editing part

    So I managed to add elements to treewidget, but only on the first column. Can anyone tell me how to put it into second one?
    Or make it as a child?

    Qt Code:
    1. #ifndef CREATEPLANET_H
    2. #define CREATEPLANET_H
    3.  
    4. #include <QtGui>
    5. #include "tree.h"
    6. class CreatePlanet : public QWidget
    7. {
    8. Q_OBJECT
    9. private:
    10.  
    11. QGridLayout *grdLayout;
    12. QTreeWidget *treeW;
    13. QSpinBox *dwNumberSpinbox;
    14. QComboBox *dwTypeCombo;
    15. QTableWidget *tableW;
    16.  
    17.  
    18. public:
    19. explicit CreatePlanet(QWidget *parent = 0);
    20. QSpinBox *sizeSpinbox;
    21. signals:
    22. void spinBoxValue(int);
    23. public slots:
    24. void applySizeButtonClicked ();
    25. void applyDwellersTypeButtonClicked();
    26. };
    27.  
    28. #endif // CREATEPLANET_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "createplanet.h"
    2.  
    3. CreatePlanet::CreatePlanet(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. QLabel *planetNameLabel = new QLabel("Name of planet:",this);
    7. QLineEdit *planetNameEdit = new QLineEdit();
    8. QLabel *sizeLabel = new QLabel("Select size of the planet:");
    9. sizeSpinbox = new QSpinBox(this);
    10. sizeSpinbox->setRange(1,7);
    11.  
    12. QPushButton *applySizeButton = new QPushButton("Apply",this);
    13.  
    14. connect(applySizeButton,SIGNAL(clicked()),this,SLOT(applySizeButtonClicked()));
    15.  
    16. treeW = new QTreeWidget(this);
    17. treeW->setColumnCount(2);
    18. treeW->setHidden(true);
    19. QStringList headers;
    20. headers << "Dwelling" << "Type";
    21. treeW->setHeaderLabels(headers);
    22.  
    23. QLabel *dwLabel = new QLabel("Select dwellings options:",this);
    24. dwNumberSpinbox = new QSpinBox(this);
    25. dwNumberSpinbox->setRange(1,1);
    26. dwTypeCombo = new QComboBox(this);
    27. dwTypeCombo->addItem("Liquid");
    28. dwTypeCombo->addItem("Etherial");
    29. dwTypeCombo->addItem("Plasma");
    30. dwTypeCombo->addItem("Solid");
    31. dwTypeCombo->addItem("Vacuum");
    32.  
    33. QPushButton *applyDwellersTypeButton = new QPushButton("Apply",this);
    34.  
    35. connect(applyDwellersTypeButton,SIGNAL(clicked()),this,SLOT(applyDwellersTypeButtonClicked()));
    36.  
    37. tableW = new QTableWidget(this);
    38. tableW->setHidden(true);
    39.  
    40. grdLayout = new QGridLayout(this);
    41.  
    42. grdLayout->addWidget(planetNameLabel,0,0,1,1);
    43. grdLayout->addWidget(planetNameEdit,0,1,1,1);
    44. grdLayout->addWidget(sizeLabel,1,0,1,1);
    45. grdLayout->addWidget(sizeSpinbox,1,1,1,1);
    46. grdLayout->addWidget(applySizeButton,2,0,1,1);
    47. grdLayout->addWidget(treeW,3,0,1,2);
    48. grdLayout->addWidget(dwLabel,4,0,1,1);
    49. grdLayout->addWidget(dwNumberSpinbox,5,0,1,1);
    50. grdLayout->addWidget(dwTypeCombo,5,1,1,1);
    51. grdLayout->addWidget(applyDwellersTypeButton,6,0,1,1);
    52. grdLayout->addWidget(tableW,7,0,1,1);
    53. this ->setLayout(grdLayout);
    54. }
    55.  
    56. void CreatePlanet::applySizeButtonClicked()
    57. {
    58. treeW->setHidden(false);
    59. treeW->clear();
    60.  
    61. dwNumberSpinbox->setRange(1,(sizeSpinbox->value()));
    62. QList<QTreeWidgetItem *> dwItems;
    63. for (int i = 1; i < sizeSpinbox->value()+1; ++i)
    64. {
    65. dwIt = new QTreeWidgetItem ((QTreeWidget*)0, QStringList(QString("Dwelling: %1").arg(i)));
    66. dwItems.append(dwIt);
    67. }
    68. treeW->insertTopLevelItems(0, dwItems);
    69.  
    70. }
    71.  
    72. void CreatePlanet::applyDwellersTypeButtonClicked()
    73. {
    74.  
    75. QTreeWidgetItem *dwTypeItem = new QTreeWidgetItem(QStringList(dwTypeCombo->currentText()));
    76. // for(int i = 1; i < dwNumberSpinbox->value();++i)
    77. //if(dwTypeItem)
    78. // dwIt->insertChild(dwNumberSpinbox->value(),dwTypeItem);
    79.  
    80. treeW->insertTopLevelItem(dwNumberSpinbox->value(),dwTypeItem);
    81.  
    82. }
    To copy to clipboard, switch view to plain text mode 

    I tried to create child, but I don't know how him to put into treewidget any ideas?
    Another problem is with is when I am putting dwTypeItem because now item is simple putted down on dwNumberSpinbox->value() index. But when I am filling lets say 3 items. then first one is added in corect place. but others in wrong order beacause I ussing insertTopLevelItem. after I put first element order of others becomes wrong. And I don't know how to correct it. I tired with if construction but nothing good happened.

  8. #8
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

    Default Re: add items to treewidget

    Quote Originally Posted by Shien View Post
    Can anyone tell me how to put it into second one?
    Did you investigate QTreeWidget::topLevelItem and QTreeWidgetItem::setText ?

    Looks to me like this should work:
    Qt Code:
    1. void CreatePlanet::applyDwellersTypeButtonClicked()
    2. {
    3. int row = dwNumberSpinbox->value()-1;
    4. QString text = dwTypeCombo->currentText();
    5. treeW->topLevelItem(row)->setText(1,text);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: add items to treewidget

    Thanks it worked.

  10. #10
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: add items to treewidget

    again problem with treewidget. last time I wanted to add value form combo box. Now I need to add spinbox value. this is best what i came up with
    Qt Code:
    1. QTreeWidgetItem *item = new QTreeWidgetItem(satelliteSizeSpinbox->value());
    2. treeW2->insertTopLevelItem(starSatelliteNumberSpinbox->value(),item);
    To copy to clipboard, switch view to plain text mode 
    and again I want to put it on second column

Similar Threads

  1. Replies: 2
    Last Post: 18th May 2010, 22:44
  2. How to add Treewidget Items when clicking on PushButton
    By mkkguru in forum Qt Programming
    Replies: 7
    Last Post: 11th February 2010, 13:48
  3. Replies: 1
    Last Post: 28th July 2009, 06:58
  4. Problem editing TreeWidget items
    By Moezzie in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2007, 21:22
  5. Replies: 2
    Last Post: 14th November 2006, 11:22

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.