Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableview change number
Forum Updated to NodeBB v4.3 + New Features

QTableview change number

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 678 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    d9f503aa-5069-467e-a8b0-b63c19cad3cb-image.png

    I am using QtableView.
    I want to change the number in the red box. What should I do?

    And I have one more question.
    For example, you have 100 values in a list variable.
    Is there a way to add the entire list instead of adding one by one with a loop statement?

    JonBJ 1 Reply Last reply
    0
    • I IknowQT

      d9f503aa-5069-467e-a8b0-b63c19cad3cb-image.png

      I am using QtableView.
      I want to change the number in the red box. What should I do?

      And I have one more question.
      For example, you have 100 values in a list variable.
      Is there a way to add the entire list instead of adding one by one with a loop statement?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @IknowQT said in QTableview change number:

      I want to change the number in the red box. What should I do?

      This comes from QTableView::horizontalHeader() (I think it;s that one, I always get confused about which header is horizontal vs vertical!). So use QTableView:setHorizontalHeader(). And/or, look at what your model's QAbstractItemModel::headerData() is returning and change that or use QAbstractItemModel::setHeaderData().

      Is there a way to add the entire list instead of adding one by one with a loop statement?

      Depends on what your model is, e.g. QStringListModel has QStringListModel::setStringList(). Yours looks like multiple columns though, you can create blank rows via insertRows() but to fill them with data you need to call setData() in a loop.

      I 1 Reply Last reply
      0
      • JonBJ JonB

        @IknowQT said in QTableview change number:

        I want to change the number in the red box. What should I do?

        This comes from QTableView::horizontalHeader() (I think it;s that one, I always get confused about which header is horizontal vs vertical!). So use QTableView:setHorizontalHeader(). And/or, look at what your model's QAbstractItemModel::headerData() is returning and change that or use QAbstractItemModel::setHeaderData().

        Is there a way to add the entire list instead of adding one by one with a loop statement?

        Depends on what your model is, e.g. QStringListModel has QStringListModel::setStringList(). Yours looks like multiple columns though, you can create blank rows via insertRows() but to fill them with data you need to call setData() in a loop.

        I Offline
        I Offline
        IknowQT
        wrote on last edited by
        #3

        @JonB

        Hmm...
        So is this even possible?
        I want to add data in units of table columns.
        Can I append data column by column using QAbstractTableModel?
        Since data is accumulated over time, it is not possible to accumulate data in the model from the beginning.

        51db00bd-e0f3-4232-95d9-f5b8e809d76b-image.png

        JonBJ 1 Reply Last reply
        0
        • I IknowQT

          @JonB

          Hmm...
          So is this even possible?
          I want to add data in units of table columns.
          Can I append data column by column using QAbstractTableModel?
          Since data is accumulated over time, it is not possible to accumulate data in the model from the beginning.

          51db00bd-e0f3-4232-95d9-f5b8e809d76b-image.png

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @IknowQT
          You did not seem to so anything about those horizontal header numbers even though that was your previous question.

          I think you are now asking whether you can add new columns to a QAbstractTableModel as you go along? Sure, there is virtual bool QAbstractItemModel::insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()). I have not used it, but I would imagine your attached QTableView has a slot on QAbstractItemModel::columnsInserted() and will redraw as required. You still have to call setData() for the content of the new columns. Yes, you have to do that in a loop, not just in one blob for a whole column, but I don't know why you seem to see that as an issue/problem.

          I 1 Reply Last reply
          0
          • JonBJ JonB

            @IknowQT
            You did not seem to so anything about those horizontal header numbers even though that was your previous question.

            I think you are now asking whether you can add new columns to a QAbstractTableModel as you go along? Sure, there is virtual bool QAbstractItemModel::insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()). I have not used it, but I would imagine your attached QTableView has a slot on QAbstractItemModel::columnsInserted() and will redraw as required. You still have to call setData() for the content of the new columns. Yes, you have to do that in a loop, not just in one blob for a whole column, but I don't know why you seem to see that as an issue/problem.

            I Offline
            I Offline
            IknowQT
            wrote on last edited by
            #5

            @JonB

            I did not understand very well.
            I'm a bit poor at English.
            I found out that it can be implemented by calling the beginInsertColumns function as a way to add columns.

                    m_pModel = new usrTableModel;
            	m_pModel->SetheaderName("Wave Length");
            	
            	m_pModel->InsertColumns(0, 1, QModelIndex(), m_pSpectrumInfo->GetWaves());
            	QModelIndex a = m_pModel->index(0, 1, QModelIndex());
            	qDebug() << m_pModel->data(m_pModel->index(0,0), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(1,0), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(2,0), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(3,0), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(4,0), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(5,0), Qt::DisplayRole);
            	
            	QList<qreal> asd; 
            	asd.append(333);
            	asd.append(334);
            	asd.append(335);
            	asd.append(336);
            	asd.append(337); 
            	asd.append(338);
            
            	m_pModel->SetRowData(asd);
                    m_pModel->InsertColumns(1, 1, QModelIndex(), asd);
            	a = m_pModel->index(0, 2, QModelIndex());
            
            	/*qDebug() << m_pModel->data(m_pModel->index(0, 1), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(1, 1), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(2, 1), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(3, 1), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(4, 1), Qt::DisplayRole);
            	qDebug() << m_pModel->data(m_pModel->index(5, 1), Qt::DisplayRole);*/
            
            	this->ui.tableWidget->setModel(m_pModel);
            	this->ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);              // 행 단위로 선택	
            

            When the first insertColums is called, the data of m_pSpectrumInfo->GetWaves() Qlist is inserted well.
            When the second InsertColumns is called, the data is overwritten on the existing row, not the row is added.
            What should I do in this case?

            In the case of QStandardItemModel, it seems that data can be inserted by accessing each row with setitem.
            Would the same be possible with QAbstractTableModel?
            The final thing I want to do is use the QVXYModelMapper to link it to the chart.

            JonBJ 1 Reply Last reply
            0
            • I IknowQT

              @JonB

              I did not understand very well.
              I'm a bit poor at English.
              I found out that it can be implemented by calling the beginInsertColumns function as a way to add columns.

                      m_pModel = new usrTableModel;
              	m_pModel->SetheaderName("Wave Length");
              	
              	m_pModel->InsertColumns(0, 1, QModelIndex(), m_pSpectrumInfo->GetWaves());
              	QModelIndex a = m_pModel->index(0, 1, QModelIndex());
              	qDebug() << m_pModel->data(m_pModel->index(0,0), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(1,0), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(2,0), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(3,0), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(4,0), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(5,0), Qt::DisplayRole);
              	
              	QList<qreal> asd; 
              	asd.append(333);
              	asd.append(334);
              	asd.append(335);
              	asd.append(336);
              	asd.append(337); 
              	asd.append(338);
              
              	m_pModel->SetRowData(asd);
                      m_pModel->InsertColumns(1, 1, QModelIndex(), asd);
              	a = m_pModel->index(0, 2, QModelIndex());
              
              	/*qDebug() << m_pModel->data(m_pModel->index(0, 1), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(1, 1), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(2, 1), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(3, 1), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(4, 1), Qt::DisplayRole);
              	qDebug() << m_pModel->data(m_pModel->index(5, 1), Qt::DisplayRole);*/
              
              	this->ui.tableWidget->setModel(m_pModel);
              	this->ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);              // 행 단위로 선택	
              

              When the first insertColums is called, the data of m_pSpectrumInfo->GetWaves() Qlist is inserted well.
              When the second InsertColumns is called, the data is overwritten on the existing row, not the row is added.
              What should I do in this case?

              In the case of QStandardItemModel, it seems that data can be inserted by accessing each row with setitem.
              Would the same be possible with QAbstractTableModel?
              The final thing I want to do is use the QVXYModelMapper to link it to the chart.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @IknowQT
              I'm afraid I don't understand what you are asking.

              When the second InsertColumns is called, the data is overwritten on the existing row, not the row is added.

              Why should inserting columns have anything to do with adding rows??

              In the case of QStandardItemModel, it seems that data can be inserted by accessing each row with setitem.

              Would the same be possible with QAbstractTableModel?

              QStandardItemModel does not do anything you cannot do yourself. setItem() --- and the whole "items" thing --- is just a layer that QStandardItemModel adds for your convenience. Ultimately it does everything via setData() and/or insertRows(), so you can do the same thing in your QAbstractTableModel-derived class.

              If you have "dedicated" methods in your QAbstractTableModel-derived class which offer things like "insert a whole column of data in one go", you do not have to call setData() and/or insertRows(). You can alter your underlying data model directly. just it must emit the required signals, like dataChanged()/begin/endInsertRows() for whatever it does on the underlying model, so that e.g. attached QTableViews know what has happened in the model.

              I 1 Reply Last reply
              2
              • JonBJ JonB

                @IknowQT
                I'm afraid I don't understand what you are asking.

                When the second InsertColumns is called, the data is overwritten on the existing row, not the row is added.

                Why should inserting columns have anything to do with adding rows??

                In the case of QStandardItemModel, it seems that data can be inserted by accessing each row with setitem.

                Would the same be possible with QAbstractTableModel?

                QStandardItemModel does not do anything you cannot do yourself. setItem() --- and the whole "items" thing --- is just a layer that QStandardItemModel adds for your convenience. Ultimately it does everything via setData() and/or insertRows(), so you can do the same thing in your QAbstractTableModel-derived class.

                If you have "dedicated" methods in your QAbstractTableModel-derived class which offer things like "insert a whole column of data in one go", you do not have to call setData() and/or insertRows(). You can alter your underlying data model directly. just it must emit the required signals, like dataChanged()/begin/endInsertRows() for whatever it does on the underlying model, so that e.g. attached QTableViews know what has happened in the model.

                I Offline
                I Offline
                IknowQT
                wrote on last edited by IknowQT
                #7

                @JonB said in QTableview change number:

                QStandardItemModel의 경우 setitem으로 각 행에 접근하여 데이터를 삽입할 수 있을 것 같습니다.

                m_pModel->InsertColumns(0, 1, QModelIndex(), m_pSpectrumInfo->GetWaves());
                m_pModel->InsertColumns(1, 1, QModelIndex(), m_pSpectrumInfo->GetWaves());
                
                void usrTableModel::InsertColumns(int nPos, int nRow, const QModelIndex& index, const QList<qreal>& data)
                {
                	beginInsertColumns(QModelIndex(), nPos, nRow+nRow-1);
                	{
                		SetRowData(data);
                	}
                	endInsertColumns();
                }
                

                I called InsertColumns twice and the result is that only one appears and overwrites with the last insert value.
                Is it possible to manage models separately for each row?
                The reason I asked this question is because it seems that speed problems may occur when the number of data increases.

                Christian EhrlicherC 1 Reply Last reply
                0
                • I IknowQT

                  @JonB said in QTableview change number:

                  QStandardItemModel의 경우 setitem으로 각 행에 접근하여 데이터를 삽입할 수 있을 것 같습니다.

                  m_pModel->InsertColumns(0, 1, QModelIndex(), m_pSpectrumInfo->GetWaves());
                  m_pModel->InsertColumns(1, 1, QModelIndex(), m_pSpectrumInfo->GetWaves());
                  
                  void usrTableModel::InsertColumns(int nPos, int nRow, const QModelIndex& index, const QList<qreal>& data)
                  {
                  	beginInsertColumns(QModelIndex(), nPos, nRow+nRow-1);
                  	{
                  		SetRowData(data);
                  	}
                  	endInsertColumns();
                  }
                  

                  I called InsertColumns twice and the result is that only one appears and overwrites with the last insert value.
                  Is it possible to manage models separately for each row?
                  The reason I asked this question is because it seems that speed problems may occur when the number of data increases.

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @IknowQT said in QTableview change number:

                  overwrites with the last insert value.

                  Because you call a custom function SetRowData() without any information where this data has to be set. Also we don't know what this function does at all.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  I 1 Reply Last reply
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    @IknowQT said in QTableview change number:

                    overwrites with the last insert value.

                    Because you call a custom function SetRowData() without any information where this data has to be set. Also we don't know what this function does at all.

                    I Offline
                    I Offline
                    IknowQT
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher
                    @JonB

                    I will post the code.

                    void wSpectrum::InitTableWidget()
                    {
                    	QList<QPair<QString, QList<qreal>>> lstTemp;
                    	QPair<QString, QList<qreal>> pairTemp;
                    
                    	pairTemp.first = "WaveLength";
                    	pairTemp.second = m_pSpectrumInfo->GetWaves();
                    	lstTemp.append(pairTemp);
                    	lstTemp.append(pairTemp);
                    
                    
                    	m_pModel = new usrTableModel(lstTemp);
                    	this->ui.tableWidget->setModel(m_pModel);
                    	
                    
                    	//m_pModel->AppendData(lstTemp);
                    	//this->ui.tableWidget->setModel(m_pModel);
                    }
                    
                    
                    ================ AbstrectTableModel ==================
                    int usrTableModel::rowCount(const QModelIndex& parent) const
                    {
                    	Q_UNUSED(parent)
                    	return m_lstInputData[0].second.count();
                    }
                    
                    int usrTableModel::columnCount(const QModelIndex& parent) const
                    {
                    	Q_UNUSED(parent)
                    	return m_lstInputData.count();
                    }
                    
                    QVariant usrTableModel::headerData(int section, Qt::Orientation orientation, int role) const
                    {
                    	//section: 행 Index 
                    	if (role != Qt::DisplayRole)
                    		return QVariant();
                    
                    	if (orientation == Qt::Horizontal)
                    	{
                    		if (m_lstInputData.count() != 0 )
                    			return m_lstInputData[section].first;
                    	}
                    	else
                    		return QString("%1").arg(section + 1);
                    }
                    
                    void usrTableModel::SetheaderName(QString strTitle)
                    {
                    	m_strTitle = strTitle;
                    }
                    
                    QVariant usrTableModel::data(const QModelIndex& index, int role) const
                    {
                    	if (role == Qt::DisplayRole)
                    	{
                    		if (m_lstInputData.count() != 0)
                    			return m_lstInputData[index.column()].second.at(index.row());
                    			//return m_lstInputData[index.column()].second->at(index.row());
                    	}
                    	else if (role == Qt::TextAlignmentRole)
                    	{
                    		return Qt::AlignCenter;
                    	}
                    	else if (role == Qt::EditRole)
                    	{
                    		return m_lstInputData[index.column()].second.at(index.row());
                    	}
                    
                    
                    	return QVariant();
                    }
                    
                    bool usrTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
                    {
                    	if (index.isValid() && role == Qt::EditRole)
                    	{
                    		m_lstInputData[index.column()].second.replace(index.row(), value.toDouble());
                    		emit dataChanged(index, index);
                    		return true;
                    	}
                    	return false;
                    }
                    
                    Qt::ItemFlags usrTableModel::flags(const QModelIndex& index) const
                    {
                    	return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
                    }
                    
                    void usrTableModel::SetRowData(const QList< QPair<QString, QList<qreal>> >& data)
                    {
                    	m_lstInputData = data;
                    }
                    
                    void usrTableModel::AppendData(const QList<QPair<QString, QList<qreal>>>& data)
                    {
                    	m_lstInputData.append(data);
                    }
                    

                    If you setModel after appending the same data, two data will appear in TableView normally.
                    I want to append data at different points in time. So I appended, but the appended data does not appear in TableView.

                    When data is updated in Qlist, I want to automatically update TableView as well.

                    I 1 Reply Last reply
                    0
                    • I IknowQT

                      @Christian-Ehrlicher
                      @JonB

                      I will post the code.

                      void wSpectrum::InitTableWidget()
                      {
                      	QList<QPair<QString, QList<qreal>>> lstTemp;
                      	QPair<QString, QList<qreal>> pairTemp;
                      
                      	pairTemp.first = "WaveLength";
                      	pairTemp.second = m_pSpectrumInfo->GetWaves();
                      	lstTemp.append(pairTemp);
                      	lstTemp.append(pairTemp);
                      
                      
                      	m_pModel = new usrTableModel(lstTemp);
                      	this->ui.tableWidget->setModel(m_pModel);
                      	
                      
                      	//m_pModel->AppendData(lstTemp);
                      	//this->ui.tableWidget->setModel(m_pModel);
                      }
                      
                      
                      ================ AbstrectTableModel ==================
                      int usrTableModel::rowCount(const QModelIndex& parent) const
                      {
                      	Q_UNUSED(parent)
                      	return m_lstInputData[0].second.count();
                      }
                      
                      int usrTableModel::columnCount(const QModelIndex& parent) const
                      {
                      	Q_UNUSED(parent)
                      	return m_lstInputData.count();
                      }
                      
                      QVariant usrTableModel::headerData(int section, Qt::Orientation orientation, int role) const
                      {
                      	//section: 행 Index 
                      	if (role != Qt::DisplayRole)
                      		return QVariant();
                      
                      	if (orientation == Qt::Horizontal)
                      	{
                      		if (m_lstInputData.count() != 0 )
                      			return m_lstInputData[section].first;
                      	}
                      	else
                      		return QString("%1").arg(section + 1);
                      }
                      
                      void usrTableModel::SetheaderName(QString strTitle)
                      {
                      	m_strTitle = strTitle;
                      }
                      
                      QVariant usrTableModel::data(const QModelIndex& index, int role) const
                      {
                      	if (role == Qt::DisplayRole)
                      	{
                      		if (m_lstInputData.count() != 0)
                      			return m_lstInputData[index.column()].second.at(index.row());
                      			//return m_lstInputData[index.column()].second->at(index.row());
                      	}
                      	else if (role == Qt::TextAlignmentRole)
                      	{
                      		return Qt::AlignCenter;
                      	}
                      	else if (role == Qt::EditRole)
                      	{
                      		return m_lstInputData[index.column()].second.at(index.row());
                      	}
                      
                      
                      	return QVariant();
                      }
                      
                      bool usrTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
                      {
                      	if (index.isValid() && role == Qt::EditRole)
                      	{
                      		m_lstInputData[index.column()].second.replace(index.row(), value.toDouble());
                      		emit dataChanged(index, index);
                      		return true;
                      	}
                      	return false;
                      }
                      
                      Qt::ItemFlags usrTableModel::flags(const QModelIndex& index) const
                      {
                      	return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
                      }
                      
                      void usrTableModel::SetRowData(const QList< QPair<QString, QList<qreal>> >& data)
                      {
                      	m_lstInputData = data;
                      }
                      
                      void usrTableModel::AppendData(const QList<QPair<QString, QList<qreal>>>& data)
                      {
                      	m_lstInputData.append(data);
                      }
                      

                      If you setModel after appending the same data, two data will appear in TableView normally.
                      I want to append data at different points in time. So I appended, but the appended data does not appear in TableView.

                      When data is updated in Qlist, I want to automatically update TableView as well.

                      I Offline
                      I Offline
                      IknowQT
                      wrote on last edited by
                      #10

                      @IknowQT said in QTableview change number:

                      @Christian-Ehrlicher
                      @JonB

                      I will post the code.

                      void wSpectrum::InitTableWidget()
                      {
                      	QList<QPair<QString, QList<qreal>>> lstTemp;
                      	QPair<QString, QList<qreal>> pairTemp;
                      
                      	pairTemp.first = "WaveLength";
                      	pairTemp.second = m_pSpectrumInfo->GetWaves();
                      	lstTemp.append(pairTemp);
                      	lstTemp.append(pairTemp);
                      
                      
                      	m_pModel = new usrTableModel(lstTemp);
                      	this->ui.tableWidget->setModel(m_pModel);
                      	
                      
                      	//m_pModel->AppendData(lstTemp);
                      	//this->ui.tableWidget->setModel(m_pModel);
                      }
                      
                      
                      ================ AbstrectTableModel ==================
                      int usrTableModel::rowCount(const QModelIndex& parent) const
                      {
                      	Q_UNUSED(parent)
                      	return m_lstInputData[0].second.count();
                      }
                      
                      int usrTableModel::columnCount(const QModelIndex& parent) const
                      {
                      	Q_UNUSED(parent)
                      	return m_lstInputData.count();
                      }
                      
                      QVariant usrTableModel::headerData(int section, Qt::Orientation orientation, int role) const
                      {
                      	//section: 행 Index 
                      	if (role != Qt::DisplayRole)
                      		return QVariant();
                      
                      	if (orientation == Qt::Horizontal)
                      	{
                      		if (m_lstInputData.count() != 0 )
                      			return m_lstInputData[section].first;
                      	}
                      	else
                      		return QString("%1").arg(section + 1);
                      }
                      
                      void usrTableModel::SetheaderName(QString strTitle)
                      {
                      	m_strTitle = strTitle;
                      }
                      
                      QVariant usrTableModel::data(const QModelIndex& index, int role) const
                      {
                      	if (role == Qt::DisplayRole)
                      	{
                      		if (m_lstInputData.count() != 0)
                      			return m_lstInputData[index.column()].second.at(index.row());
                      			//return m_lstInputData[index.column()].second->at(index.row());
                      	}
                      	else if (role == Qt::TextAlignmentRole)
                      	{
                      		return Qt::AlignCenter;
                      	}
                      	else if (role == Qt::EditRole)
                      	{
                      		return m_lstInputData[index.column()].second.at(index.row());
                      	}
                      
                      
                      	return QVariant();
                      }
                      
                      bool usrTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
                      {
                      	if (index.isValid() && role == Qt::EditRole)
                      	{
                      		m_lstInputData[index.column()].second.replace(index.row(), value.toDouble());
                      		emit dataChanged(index, index);
                      		return true;
                      	}
                      	return false;
                      }
                      
                      Qt::ItemFlags usrTableModel::flags(const QModelIndex& index) const
                      {
                      	return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
                      }
                      
                      void usrTableModel::SetRowData(const QList< QPair<QString, QList<qreal>> >& data)
                      {
                      	m_lstInputData = data;
                      }
                      
                      void usrTableModel::AppendData(const QList<QPair<QString, QList<qreal>>>& data)
                      {
                      	m_lstInputData.append(data);
                      }
                      

                      If you setModel after appending the same data, two data will appear in TableView normally.
                      I want to append data at different points in time. So I appended, but the appended data does not appear in TableView.

                      When data is updated in Qlist, I want to automatically update TableView as well.

                      void usrTableModel::AppendData(const QList<QPair<QString, QList<qreal>>>& data)
                      {
                      	QModelIndex a = QModelIndex();
                      	beginInsertColumns(QModelIndex(), m_lstInputData.count(), 1);
                      	m_lstInputData.append(data);
                      	endInsertColumns();
                      }
                      

                      I tried to do this, but the value of QModelIndex comes out as -1.

                      I 1 Reply Last reply
                      0
                      • I IknowQT

                        @IknowQT said in QTableview change number:

                        @Christian-Ehrlicher
                        @JonB

                        I will post the code.

                        void wSpectrum::InitTableWidget()
                        {
                        	QList<QPair<QString, QList<qreal>>> lstTemp;
                        	QPair<QString, QList<qreal>> pairTemp;
                        
                        	pairTemp.first = "WaveLength";
                        	pairTemp.second = m_pSpectrumInfo->GetWaves();
                        	lstTemp.append(pairTemp);
                        	lstTemp.append(pairTemp);
                        
                        
                        	m_pModel = new usrTableModel(lstTemp);
                        	this->ui.tableWidget->setModel(m_pModel);
                        	
                        
                        	//m_pModel->AppendData(lstTemp);
                        	//this->ui.tableWidget->setModel(m_pModel);
                        }
                        
                        
                        ================ AbstrectTableModel ==================
                        int usrTableModel::rowCount(const QModelIndex& parent) const
                        {
                        	Q_UNUSED(parent)
                        	return m_lstInputData[0].second.count();
                        }
                        
                        int usrTableModel::columnCount(const QModelIndex& parent) const
                        {
                        	Q_UNUSED(parent)
                        	return m_lstInputData.count();
                        }
                        
                        QVariant usrTableModel::headerData(int section, Qt::Orientation orientation, int role) const
                        {
                        	//section: 행 Index 
                        	if (role != Qt::DisplayRole)
                        		return QVariant();
                        
                        	if (orientation == Qt::Horizontal)
                        	{
                        		if (m_lstInputData.count() != 0 )
                        			return m_lstInputData[section].first;
                        	}
                        	else
                        		return QString("%1").arg(section + 1);
                        }
                        
                        void usrTableModel::SetheaderName(QString strTitle)
                        {
                        	m_strTitle = strTitle;
                        }
                        
                        QVariant usrTableModel::data(const QModelIndex& index, int role) const
                        {
                        	if (role == Qt::DisplayRole)
                        	{
                        		if (m_lstInputData.count() != 0)
                        			return m_lstInputData[index.column()].second.at(index.row());
                        			//return m_lstInputData[index.column()].second->at(index.row());
                        	}
                        	else if (role == Qt::TextAlignmentRole)
                        	{
                        		return Qt::AlignCenter;
                        	}
                        	else if (role == Qt::EditRole)
                        	{
                        		return m_lstInputData[index.column()].second.at(index.row());
                        	}
                        
                        
                        	return QVariant();
                        }
                        
                        bool usrTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
                        {
                        	if (index.isValid() && role == Qt::EditRole)
                        	{
                        		m_lstInputData[index.column()].second.replace(index.row(), value.toDouble());
                        		emit dataChanged(index, index);
                        		return true;
                        	}
                        	return false;
                        }
                        
                        Qt::ItemFlags usrTableModel::flags(const QModelIndex& index) const
                        {
                        	return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
                        }
                        
                        void usrTableModel::SetRowData(const QList< QPair<QString, QList<qreal>> >& data)
                        {
                        	m_lstInputData = data;
                        }
                        
                        void usrTableModel::AppendData(const QList<QPair<QString, QList<qreal>>>& data)
                        {
                        	m_lstInputData.append(data);
                        }
                        

                        If you setModel after appending the same data, two data will appear in TableView normally.
                        I want to append data at different points in time. So I appended, but the appended data does not appear in TableView.

                        When data is updated in Qlist, I want to automatically update TableView as well.

                        void usrTableModel::AppendData(const QList<QPair<QString, QList<qreal>>>& data)
                        {
                        	QModelIndex a = QModelIndex();
                        	beginInsertColumns(QModelIndex(), m_lstInputData.count(), 1);
                        	m_lstInputData.append(data);
                        	endInsertColumns();
                        }
                        

                        I tried to do this, but the value of QModelIndex comes out as -1.

                        I Offline
                        I Offline
                        IknowQT
                        wrote on last edited by
                        #11

                        @IknowQT said in QTableview change number:

                        @IknowQT said in QTableview change number:

                        @Christian-Ehrlicher
                        @JonB

                        I will post the code.

                        void wSpectrum::InitTableWidget()
                        {
                        	QList<QPair<QString, QList<qreal>>> lstTemp;
                        	QPair<QString, QList<qreal>> pairTemp;
                        
                        	pairTemp.first = "WaveLength";
                        	pairTemp.second = m_pSpectrumInfo->GetWaves();
                        	lstTemp.append(pairTemp);
                        	lstTemp.append(pairTemp);
                        
                        
                        	m_pModel = new usrTableModel(lstTemp);
                        	this->ui.tableWidget->setModel(m_pModel);
                        	
                        
                        	//m_pModel->AppendData(lstTemp);
                        	//this->ui.tableWidget->setModel(m_pModel);
                        }
                        
                        
                        ================ AbstrectTableModel ==================
                        int usrTableModel::rowCount(const QModelIndex& parent) const
                        {
                        	Q_UNUSED(parent)
                        	return m_lstInputData[0].second.count();
                        }
                        
                        int usrTableModel::columnCount(const QModelIndex& parent) const
                        {
                        	Q_UNUSED(parent)
                        	return m_lstInputData.count();
                        }
                        
                        QVariant usrTableModel::headerData(int section, Qt::Orientation orientation, int role) const
                        {
                        	//section: 행 Index 
                        	if (role != Qt::DisplayRole)
                        		return QVariant();
                        
                        	if (orientation == Qt::Horizontal)
                        	{
                        		if (m_lstInputData.count() != 0 )
                        			return m_lstInputData[section].first;
                        	}
                        	else
                        		return QString("%1").arg(section + 1);
                        }
                        
                        void usrTableModel::SetheaderName(QString strTitle)
                        {
                        	m_strTitle = strTitle;
                        }
                        
                        QVariant usrTableModel::data(const QModelIndex& index, int role) const
                        {
                        	if (role == Qt::DisplayRole)
                        	{
                        		if (m_lstInputData.count() != 0)
                        			return m_lstInputData[index.column()].second.at(index.row());
                        			//return m_lstInputData[index.column()].second->at(index.row());
                        	}
                        	else if (role == Qt::TextAlignmentRole)
                        	{
                        		return Qt::AlignCenter;
                        	}
                        	else if (role == Qt::EditRole)
                        	{
                        		return m_lstInputData[index.column()].second.at(index.row());
                        	}
                        
                        
                        	return QVariant();
                        }
                        
                        bool usrTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
                        {
                        	if (index.isValid() && role == Qt::EditRole)
                        	{
                        		m_lstInputData[index.column()].second.replace(index.row(), value.toDouble());
                        		emit dataChanged(index, index);
                        		return true;
                        	}
                        	return false;
                        }
                        
                        Qt::ItemFlags usrTableModel::flags(const QModelIndex& index) const
                        {
                        	return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
                        }
                        
                        void usrTableModel::SetRowData(const QList< QPair<QString, QList<qreal>> >& data)
                        {
                        	m_lstInputData = data;
                        }
                        
                        void usrTableModel::AppendData(const QList<QPair<QString, QList<qreal>>>& data)
                        {
                        	m_lstInputData.append(data);
                        }
                        

                        If you setModel after appending the same data, two data will appear in TableView normally.
                        I want to append data at different points in time. So I appended, but the appended data does not appear in TableView.

                        When data is updated in Qlist, I want to automatically update TableView as well.

                        void usrTableModel::AppendData(const QList<QPair<QString, QList<qreal>>>& data)
                        {
                        	QModelIndex a = QModelIndex();
                        	beginInsertColumns(QModelIndex(), m_lstInputData.count(), 1);
                        	m_lstInputData.append(data);
                        	endInsertColumns();
                        }
                        

                        I tried to do this, but the value of QModelIndex comes out as -1.

                        void usrTableModel::AppendData(const QPair<QString, QList<qreal>>& data)
                        {
                        	m_lstInputData.append(data);
                        	int nNewCol = this->columnCount();
                        	beginInsertColumns(QModelIndex(), nNewCol, nNewCol);
                        	endInsertColumns();
                        
                        	QModelIndex a = QModelIndex();
                        	QModelIndex index = createIndex(m_lstInputData[0].second.count(), m_lstInputData.count());
                        	emit dataChanged(index, index);
                        }
                        

                        I implemented it like this and it prints normally.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved