Results 1 to 6 of 6

Thread: C++QT How to change size grid when index in qcombobox is changed

  1. #1
    Join Date
    Sep 2015
    Posts
    22
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default C++QT How to change size grid when index in qcombobox is changed

    im trying to create a matrix grid that changes its size when an option in the combobox is selected, i dont know how to call the function that creates the grid everytime i change the value of the combo box...



    Qt Code:
    1. void MatrixOP::createGridMatrixSize()
    2. {
    3. setMatrixSize=new QGroupBox(tr("Select Size"));
    4. QGridLayout *layout =new QGridLayout;
    5. QLabel *sizeLabel = new QLabel(tr("Size"));
    6. QComboBox *chooseSize = new QComboBox;
    7. chooseSize->addItems(QStringList()<<"0"<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9"<<"10");
    8. chooseSize->setCurrentIndex(1);
    9. layout->addWidget(sizeLabel, 0,0);
    10. layout->addWidget(chooseSize, 0,1);
    11. setMatrixSize->setLayout(layout);
    12. }
    13.  
    14. void MatrixOP::createGridMatrixValues()
    15. {
    16. int gridSize=chooseSize->currentIndex();
    17. setMatrixValues = new QGroupBox (tr("Input Values"));
    18. QGridLayout *layout = new QGridLayout;
    19. for(int j=1; j<=gridSize;j++){
    20. for (int i=1; i<=gridSize;i++){
    21. matrixValues[i]=new QLineEdit;
    22. layout->addWidget(matrixValues[i], j, i+1);
    23. }
    24. }
    25. setMatrixValues->setLayout(layout);
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    i believe it can be done with SIGNALS and SLOTS but i cant figure out the way to refresh the mainwindow to "redraw" the grid....any help?thx

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++QT How to change size grid when index in qcombobox is changed

    If you make createGridMatrixValues() a slot, you can connect it to the chooseSize combobox's change signals, e.g. currentIndexChanged.

    In creteGridMatrixValues() you first delete the current setMatrixValues widgets, create it anew using your new input and then re-add it to its parent's layout.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    22
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: C++QT How to change size grid when index in qcombobox is changed

    i cant make it work, im trying to delete the array of widgets when the slot is called, it compiles but when i change the value in the combo box y get a crash..

    Qt Code:
    1. connect(chooseSize, SIGNAL(activated(int)), this, SLOT(on_changeIndex_chooseSize(int)));
    2.  
    3. QVBoxLayout *mainLayout = new QVBoxLayout;
    4. mainLayout->addWidget(setMatrixSize);
    5. mainLayout->addWidget(setMatrixValues);
    6. mainLayout->addWidget(chooseSize);
    7. setLayout(mainLayout);
    8.  
    9.  
    10. void MatrixOP::createGridMatrixValues(int size)
    11. {
    12. setMatrixValues = new QGroupBox (tr("Input Values"));
    13. QGridLayout *layout = new QGridLayout;
    14. matrixValues=new QLineEdit* [size];
    15. for(int j=1; j<=size;j++){
    16. for (int i=1; i<=size;i++){
    17. matrixValues[i]=new QLineEdit[size];
    18. layout->addWidget(matrixValues[i], j, i+1);
    19. }
    20. }
    21.  
    22. setMatrixValues->setLayout(layout);
    23.  
    24. }
    25.  
    26. void MatrixOP::on_changeIndex_chooseSize(int index)
    27. {
    28. for (int i=0;i<matrixSize;i++){
    29. delete [] matrixValues[i];
    30. }
    31. delete[]matrixValues;
    32. createGridMatrixValues(index);
    33. }
    To copy to clipboard, switch view to plain text mode 

    any helps?
    Last edited by anda_skoa; 10th October 2015 at 10:45. Reason: changed [qtclass] to [code]

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++QT How to change size grid when index in qcombobox is changed

    You are using the delete[] operator on an normal pointer inside the loop.

    If you use a QVector instead of a plain C array, you can just use qDeleteAll() on the container and then clear() it.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2015
    Posts
    22
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: C++QT How to change size grid when index in qcombobox is changed

    thanks for the help, i change my array w/ a Qvector<qvector<qlineEdit>> array but i still cant figure out how to use qDeleteAll() to remove the created widgets and add a new ones...

    Qt Code:
    1. void MatrixOP::createGridMatrixValues(int size)
    2. {
    3. setMatrixValues = new QGroupBox (tr("Input Values"));
    4. QGridLayout *layout = new QGridLayout;
    5. qDebug()<<size;
    6. for(int j=1; j<=size+1;j++){
    7. QVector<QLineEdit *> foo;
    8. for (int i=1; i<=size+1;i++){
    9. QLineEdit *value =new QLineEdit(this);
    10. foo.append(value);
    11. layout->addWidget(value, j, i);
    12. }
    13. vectorOfVectorOfLineEdits.append(foo);
    14. }
    15.  
    16. setMatrixValues->setLayout(layout);
    17. }
    18. void MatrixOP::on_changeIndex_chooseSize(int index)
    19. {
    20. vectorOfVectorOfLineEdits.clear();
    21. createGridMatrixValues(index);
    22. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++QT How to change size grid when index in qcombobox is changed

    Well, one thing that is definitely wrong is creation of the groupbox every time.
    Also missing the call to delete the layout.

    As for qDeleteAll(), since you have a vector of vectors, you'll need to iterate over the outer vector and call qDeleteAll on each element.
    qDeleteAll() iterates over that inner vector then an calls delete on each element.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 17th April 2014, 13:12
  2. Change elements size in a grid
    By asieriko in forum Qt Quick
    Replies: 4
    Last Post: 5th January 2014, 13:23
  3. Grid of QComboBox
    By Rakma74 in forum Newbie
    Replies: 12
    Last Post: 15th August 2012, 10:16
  4. Index (position) changed on saving with QTableView
    By Auryn in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2008, 09:15
  5. Value from QComboBox instead of Index
    By arunvv in forum Qt Programming
    Replies: 2
    Last Post: 17th May 2007, 23:24

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.