[Qt-interest] How to reverse the order of the rows in a QTableWidget

Malyushytsky, Alex alex at wai.com
Tue Sep 21 04:05:44 CEST 2010


If you need reversing, not sorting you can do it without temporary storage by:
1. Create loop from 0 to ½ of number of rows.
2. Determine which pair of rows  you want to swap data row, row2 ( 0 , numRows()-1  for example)
3. For each column ( loop by columns) use takeItem to remove both cell items from table:
       Look in documentation for QTableWidgetItem * QTableWidget::takeItem ( int row, int column )
4. now you have set items you already took out from the QTableWidget with
        void QTableWidget::setItem ( int row, int column, QTableWidgetItem * item )

But if you really want sorting I would suggest you to look at the sorting feature instead.

Regards,
   Alex


From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Bijay Panda
Sent: Thursday, September 16, 2010 9:26 PM
To: QT Interest List
Subject: [Qt-interest] How to reverse the order of the rows in a QTableWidget

Hi All,

I am working on QT 4.5.2 on linux.I want to reverse the order of rows in my QTableWidget on a pushButton click outside the QTableWidget   ( means 1-10 to 10-1).This is the ay I am doing..But its not working..Actually it's unable to remove the cellWidget.

  connect(m_pcBtnReverse,SIGNAL(clicked(bool)),this,SLOT(reverseRouteOnClick(bool)));

  bool RoutesGUI::reverseRouteOnClick(bool bIndex_)
  {
    /*Reverses order of rows i.e set the item in descending if its in ascending order*/
    int usiRowCount = m_pcTblInput->rowCount();

    int usiCol = 2;

    /*Gets the contents of all the row from GUI table by passing the total row number and coloumn number*/
    char*** pppchRows = (char***)calloc (usiRowCount, sizeof(char**));

    /*Get all the records from GUI table*/
    for( int i = m_pcTblInput->rowCount()-1;i>= 0;i--)
    {
        pppchRows[i] = getRow(i);
    }

    /*Remove all the cell widget from GUI table*/
    for( int i = m_pcTblInput->rowCount()-1; i >= 0;i--)
    {
        for( int j = 0;j< 2;j++)
        { cout<<"Removing cellwidget:"<<endl; m_pcTblInput->removeCellWidget(i, j); }

    }

    /*Set items in reverse order into the table*/
    for( int i = 0;i< m_pcTblInput->rowCount();i++)
    {
        QTableWidgetItem* pcTblItem;

        for( int j = 0;j< 2;j++)
        {
            cout<<"GUI::reverseRouteOnClick : row : "<< i <<" and column : "<< j <<"  is : " << pppchRows[i][j]<<endl;
            /*Set the item to Table for column 0 and 1 */
            pcTblItem = new QTableWidgetItem (tr(pppchRows[i][j]));
            m_pcTblInput->setItem(i, j, pcTblItem);

        }
    }
}


Any help is appreciated.Thanx in advace

--
Best Regards

Bijay Kumar Panda
Mob-9739591816



---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”




More information about the Qt-interest-old mailing list