T
T
theSever2018-03-12 22:42:20
Qt
theSever, 2018-03-12 22:42:20

Qt C++ listWidget how to highlight the first line and get the text of the selected line?

It is necessary after the loading into the list of strings, immediately select 1 element. How?

// Очистить список строк
    ui->listWidget->clear();

    //Осуществляем запрос
    QSqlQuery query;
    query.exec("select name from sqlite_master where type='table'");

    while (query.next())
    {
    QString table = query.value(0).toString();
    ui->listWidget->addItem(table);
    }

And how to get the text of the selected element?
// Обновить таблицу
void MainWindow::table_now()
{
    // Очистить таблицу
    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);

Получить текст выделенного элемента!

    // Вывод в таблицу
    QSqlQuery query;
    query.exec("select id,name,comment from `Текст Элемента`");
    while (query.next())
    {
        ui->tableWidget->insertRow(ui->tableWidget->rowCount());
        for (int i=0; i<3; i++){
            QTableWidgetItem * item = new QTableWidgetItem(query.value(i).toString());
            ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,i,item);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacob E, 2018-03-12
@theSever

It is necessary after the loading into the list of strings, immediately select 1 element. How?
QListWidget::setCurrentRow ?
And how to get the text of the selected element?
QListWidget::currentItem ?
Maybe before writing code, read the documentation , or even a tutorial?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question