PDA

View Full Version : How to get text for Tableview to Combobox ?



hohoanganh205
26th December 2011, 10:49
I loaded a Form with tableview have data (data loaded from MYSQL ), when a row on tableview is selected, data from each column will display on LineEdit ( Mã DL, Tên DL, Email, Địa chỉ, Điện thoại, Tiền nợ ) and also display on DateEdit ( Ngà y nháº*n ) but can not display on 2 Combobox ( Loại DL, Tên quáº*n ). See pic :

7203


Combobox "Loại DL, Tên quáº*n" display " 1, Quan 1" is default Items, can not diaplay for tableview. These are code :




connect(ui.tableView->selectionModel(),SIGNAL(selectionChanged(const QItemSelection &,const QItemSelection &)),this,SLOT(selectItem(const QItemSelection &,const QItemSelection &)));
ui.tableView->selectRow(0);


void QuanLyDaiLy::selectItem(QItemSelection selected,QItemSelection)
{
QSqlQueryModel *QModel;
QModelIndexList items = selected.indexes();
ui.txtMaDL->setText(QModel->data(items[0],Qt::DisplayRole).toString());
ui.txtTenDL->setText(QModel->data(items[1],Qt::DisplayRole).toString());
ui.txtEmail->setText(QModel->data(items[2],Qt::DisplayRole).toString());
ui.txtDiaChi->setText(QModel->data(items[3],Qt::DisplayRole).toString());

QModelIndexList mySelection4 = ui.tableView->selectionModel()->selectedRows(4);
QDate data4;
foreach(QModelIndex selectedIndex4, mySelection4)
data4 = selectedIndex4.data(0).toDate();
ui.dtNgayNhan->setDate(data4);

ui.cbLoaiDL->itemText(QModel->data(items[5],Qt::DisplayRole).toInt());
ui.cbTenQuan->setCurrentIndex(QModel->data(items[6],Qt::DisplayRole).toInt());

ui.txtDienThoai->setText(QModel->data(items[7],Qt::DisplayRole).toString());
ui.txtTienNo->setText(QModel->data(items[8],Qt::DisplayRole).toString());
}



ui.cbLoaiDL->itemText(...
ui.cbTenQuan->setCurrentIndex(..

itemText() and setCurrentIndex() functions are not work , which functions I can use ?

Thanks for help !

Lykurg
26th December 2011, 10:54
See QDataWidgetMapper, which is the "right" way to do what you want.

And no wonder that setCurrentIndes is not working. Debug what QModel->data(items[6],Qt::DisplayRole) returns! It is "quan 2" which is not a number....