I try to click on the header to order that column of elements, but I can not detect the signal. Does anyone know how to fix it?

1

connect(tableView->horizontalHeader(), SIGNAL(clicked(int)), this, SLOT(ordenar(int)));

in the header declared

private slots:

void ordenar(int logicalIndex);

void MainWindow::ordenar(int logicalIndex )
{



    QString id;
    mModel=new QSqlQueryModel(this);

    if (logicalIndex==1){id="grupo";}
    if (logicalIndex==2){id="tipo";}
    if (logicalIndex==3){id="nombre";}
    if (logicalIndex==4){id="descripcion";}
    else{
        id="id";
    }
    QString campo=comboCampo->currentText();
    QString cond=comboCondicion->currentText();
    QString valor=comboValor->currentText();
    QString multi=comboMulti->currentText();
    QString campo2=comboCampo2->currentText();
    QString cond2=comboCondicion2->currentText();
    QString valor2=comboValor2->currentText();
    if(filtro==1){
        if(comboMulti->currentIndex()!=0){
            mModel->setQuery("SELECT * FROM vertical WHERE " +campo+ " "+cond +" '"+valor+"' " +multi+" " +campo2+ " "+cond2 +" '"+valor2+"' ORDER BY '"+id+"';");}
        else {mModel->setQuery("SELECT * FROM vertical WHERE " +campo+ " "+cond +" '"+valor+"' ORDER BY '"+id+"';");}
    }
    if(filtro!=1){
        mModel->setQuery("SELECT id,grupo,tipo,nombre,descripcion,icono from vertical ORDER BY '"+id+"';");
    }
    mModel->setHeaderData(0,Qt::Horizontal,tr("ID"));
    mModel->setHeaderData(1,Qt::Horizontal,tr("GRUPO"));
    mModel->setHeaderData(2,Qt::Horizontal,tr("TIPO"));
    mModel->setHeaderData(3,Qt::Horizontal,tr("NOMBRE"));
    mModel->setHeaderData(4,Qt::Horizontal,tr("DESCRIPCI\u00D3N"));
    mModel->setHeaderData(5,Qt::Horizontal,tr("ICONO"));

    tableView->setModel(mModel);
    tableView->resizeColumnsToContents();
    tableView->show();


}

This is what I get by console when I click on the header of the table:

  

Object :: connect: No such signal QHeaderView :: clicked () in .. \ signal_dev \ mainwindow.cpp: 362

    
asked by Antonio Veloso Gomez 12.05.2017 в 08:58
source

1 answer

1

If you check the Qt documentation, you will see that QHeaderView does not have a signal clicked() .

How column sorting is done?

The first thing is to enable sorting in QTableView . To do this you have to call setSortingEnabled() :

QTableView * tableView;
tableView->setSortingEnabled(true);

This enables the sort buttons in the headers of the table ... but in case you only see that it does not do anything. By the way, this property can be modified in the form editor of Qt.

Additionally, the order is usually made through a proxy . The system of Qt models is usually structured in the following way:

  • Main model: contains the data properly structured (is the one you are currently filling)
  • Proxy: It is located between the main model and the view. Manage sorting and filtering operations. By not touching the principial model the programming is greatly simplified.
  • View: Receive a model or a proxy and present the information it offers.

If you do not need a specific ordering of the data you can use one of the proxys that Qt Bring default:

TableView* tableView;
QSqlQueryModel* mModel;
// ...

tableView->setSortingEnabled(true);
QSortFilterProxyModel* proxy = new QSortFilterProxyModel(this);
proxy->setSourceModel(mModel);

tableView->setModel(proxy);

After this configuration the data in the table should be able to be sorted automatically when the table headers are moved.

    
answered by 12.05.2017 / 09:20
source