Results 1 to 7 of 7

Thread: Dynamically configured tableview

  1. #1
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Dynamically configured tableview

    I want to read measured data from a device. At first the device tells me what data I can read from it, then the user is able to select the data he wants to measure. He can set an intervall time for measuring and then start the process.



    Qt Code:
    1. //"monitorwindow.h"
    2. QStringList AvailableData;
    3. QCheckBox *DataCheckbox[];
    To copy to clipboard, switch view to plain text mode 

    This is the first proto type of the list that will be created (because communication to the device isn't ready yet):

    Qt Code:
    1. void MonitorWindow::on_CreateListButton_clicked(){
    2. QVBoxLayout* verticalLayout = new QVBoxLayout;
    3.  
    4. AvailableData.append("Temp1");
    5. AvailableData.append("Temp2");
    6. AvailableData.append("Temp3");
    7. AvailableData.append("Temp4");
    8.  
    9. QLabel* tempLabel[AvailableData.count()];
    10. QHBoxLayout* horizontalLayout[AvailableData.count()];
    11.  
    12. for (int i = 0; i< AvailableData.count(); i++){
    13. DataCheckbox[i] = new QCheckBox;
    14. tempLabel[i] = new QLabel;
    15. horizontalLayout[i] = new QHBoxLayout;
    16.  
    17. connect(DataCheckbox[i],SIGNAL(clicked()),SLOT(setTransducerFlags()));
    18. horizontalLayout[i]->addWidget(DataCheckbox[i]);
    19.  
    20. tempLabel[i]->setText(AvailableData.at(i));
    21. horizontalLayout[i]->addWidget(tempLabel[i]);
    22.  
    23. horizontalLayout[i]->setStretch(1,9);
    24. verticalLayout->addLayout(horizontalLayout[i]);
    25. }
    26. ui->groupBox->setLayout(verticalLayout);
    27. }
    To copy to clipboard, switch view to plain text mode 

    On the basis of this list I want to create a tableview that shows me the selected data in different columns while measuring. But I really have no idea of creating a tableview dynamically, I always worked on the base of a model (that already was created).

    So step by step. How can I add a new column and set the columnheader text to AvailableData.at(i)?

    Thank You!

  2. #2
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically configured tableview

    Its not clear to me what "dynamically" means here.

    Is there a problem with storing the data generated by the device in a QAbstractTableModel and set a QTableView on this model?

  3. #3
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically configured tableview

    I solved the problem on my own, thank you. Maybe dynamically is the wrong word. Sorry.


    My next problem is: The way i created the CheckBox seems to be wrong. I want a global declared CheckBox because in other functions I need to check if the CheckBox[i] is checked or enable/disable them. I get to know how many CheckBox I need while runtime.
    Header:
    Qt Code:
    1. QCheckBox *DataCheckbox[];
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MonitorWindow::on_CreateListButton_clicked(){
    2. AvailableData.append("Temp1");
    3. AvailableData.append("Temp2");
    4. AvailableData.append("Temp3");
    5. AvailableData.append("Temp4");
    6.  
    7. for (int i = 0; i< AvailableData.count(); i++){
    8. DataCheckbox[i] = new QCheckBox;
    9. ...
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    How do I declare it right? Because this seems to be wrong: If i click on the button everything works fine but when I close the last window of my application, my debug says that the program is crashed and I dont get my return 0.

  4. #4
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically configured tableview

    So what you are doing is allocating memory for the QCheckboxes in heap without a parent and not freeing it? I am not surprised it crashes..

    Ps: I would recommend against using global checkboxes. There is always a better way if you need it in multiple places.

  5. #5
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically configured tableview

    oh I copied the wrong Code. In my code i set the parent (this) but it still crashes. What do you mean with freeing?

    Thank You. But what is the better way? How can I check for example if the Box is checked in another function?

  6. #6
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically configured tableview

    • Ok, so you are creating the checkbox as a child of MonitorWindow. DataCheckbox[i] = new QCheckBox(this);
    • But there are other classes that access its status.
    • When the Monitor window destructor is called and the memory is freed, other classes might be trying to check the checkbox status! So the crash.



    This is exactly why global objects should be avoided. I cannot suggest another approach without seeing your code, but ideally you need to create this checkbox in say Monitor window and the classes that need access should have a pointer to Monitor window.
    Then when they need to check the status they can call monitorWindowObj->GetCheckboxStatus().

  7. #7
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically configured tableview

    Monitor Window is the only class who should have access to the CheckBoxes. I declared it private.

    So here a small example of how I use the checkboxes in other functions:

    Qt Code:
    1. void MonitorWindow::on_CreateListButton_clicked(){
    2. QCheckBox *DataCheckbox[AvailableData.count()];
    3.  
    4. for (int i = 0; i < AvailableData.count(); i++){
    5. DataCheckbox[i] = new QCheckBox(this);
    6. connect(DataCheckbox[i],SIGNAL(clicked()),SLOT(setTransducerFlags()));
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void MonitorWindow::setTransducerFlags(){
    2. for (int i = 0; i< AvailableData.count(); i++){
    3. if(DataCheckbox[i]->isChecked()){
    4. ...
    5. }
    6. else{
    7. ...
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MonitorWindow::startRecord(){
    2. ...
    3. for (int i = 0; i < AvailableData.count(); i++){
    4. DataCheckbox[i]->setEnabled(false);
    5. }
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Gdb and Cdb not configured pproperly
    By poporacer in forum Newbie
    Replies: 2
    Last Post: 20th March 2012, 23:06
  2. dynamically add rows to tableview
    By maarvi in forum Newbie
    Replies: 4
    Last Post: 29th June 2011, 08:25
  3. How to know the options with which Qt is configured?
    By kapoorsudhish in forum Installation and Deployment
    Replies: 2
    Last Post: 9th October 2010, 14:26
  4. Qt4 configured with -largefile
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2007, 23:08
  5. QMAKESPEC has not been configured
    By karthik in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 18th November 2007, 18:54

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.