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 add items to treewidget

    Hi,

    I have tree widget with 2 columns. But I don't know how to insert there data.

    With spin box I want to choose row and then choosing from combo box value insert it into selected row. What would be easiest way to do this? Because I tried all day and nothing good happened.

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

    Default Re: add items to treewidget

    What exactly is your problem?
    Inserting items to the tree?
    Combining the the spinbox and combo and tree?
    And then in detail - what part?
    If you want help, then please post enough information about the problem you have, what you have tried, the errors you get ect.
    No one has time to try and guess what you have done, and what not.

    Because I tried all day and nothing good happened.
    This has no meaningful information to someone who wants to help you.
    ==========================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

    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.  
    16.  
    17. public:
    18. explicit CreatePlanet(QWidget *parent = 0);
    19. QSpinBox *sizeSpinbox;
    20. signals:
    21. void spinBoxValue(int);
    22. public slots:
    23. void applySizeButtonClicked ();
    24. void applyDwellersTypeButtonClicked();
    25. };
    26.  
    27. #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->setHidden(true);
    18. treeW->setColumnCount(2);
    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. grdLayout = new QGridLayout(this);
    38.  
    39. grdLayout->addWidget(planetNameLabel,0,0,1,1);
    40. grdLayout->addWidget(planetNameEdit,0,1,1,1);
    41. grdLayout->addWidget(sizeLabel,1,0,1,1);
    42. grdLayout->addWidget(sizeSpinbox,1,1,1,1);
    43. grdLayout->addWidget(applySizeButton,2,0,1,1);
    44. grdLayout->addWidget(treeW,3,0,1,2);
    45. grdLayout->addWidget(dwLabel,4,0,1,1);
    46. grdLayout->addWidget(dwNumberSpinbox,5,0,1,1);
    47. grdLayout->addWidget(dwTypeCombo,5,1,1,1);
    48. grdLayout->addWidget(applyDwellersTypeButton,6,0,1,1);
    49. this ->setLayout(grdLayout);
    50. }
    51.  
    52. void CreatePlanet::applySizeButtonClicked()
    53. {
    54. treeW->setHidden(false);
    55. treeW->clear();
    56.  
    57. dwNumberSpinbox->setRange(1,(sizeSpinbox->value()));
    58. QList<QTreeWidgetItem *> dwItems;
    59. for (int i = 1; i < sizeSpinbox->value()+1; ++i)
    60. {
    61. dwItems.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("Dwelling %1").arg(i))));
    62. }
    63. treeW->insertTopLevelItems(0, dwItems);
    64.  
    65. }
    66.  
    67. void CreatePlanet::applyDwellersTypeButtonClicked()
    68. {
    69.  
    70. }
    To copy to clipboard, switch view to plain text mode 

    ApplyDwellersTypeButton slot shoud use values from dwNumberSpinbox as key where to put dwTypeCombo value.

    This is the hardest way, I don't know how to do this.
    Can anyone help me?

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

    Default Re: add items to treewidget

    I don't know how to do this.
    Do what?
    Connect a signal to slot?
    Again, please explain what the problem is that you have!
    ==========================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
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: add items to treewidget

    When I choose values from dwNumberSpinbox and dwTypeCombo and cliking apply. It should put dwTypeCombo's value to row(dwNumberSpinbox.value) in tree widget's second column.

    thats the idea.

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

    Default Re: add items to treewidget

    Ok, so now we know what you want it to do.
    And what is the problem you have?
    Note: problem != "should do".
    ==========================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.

  7. #7
    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

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

    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.

  9. #9
    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?

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

    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.

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

    Default Re: add items to treewidget

    May be QTreeWidget::insertTopLevelItem will help you

  12. #12
    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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.