I am trying to obscure passwords in my Qtableview
in Qt. I tried :
QTableView view;
View.setItemDelegateForColumn(8,());
Also :
ui->tableView->setColumnhidden(8,true);
I know it's not right, just what I was trying to hide/obscure column 8.
CodePudding user response:
You can store password in item data. Something like this:
QString getPassword(QTableWidgetItem *itm)
{
return itm->data(Qt::UserRole).toString();
}
void hidePassword(QTableWidgetItem *itm)
{
QString password = getPassword(itm);
password = password.fill('*', password.length());
itm->setText(password);
itm->setData(Qt::AccessibleTextRole, false);
}
void showPassword(QTableWidgetItem *itm)
{
QString password = getPassword(itm);
itm->setText(password);
itm->setData(Qt::AccessibleTextRole, true);
}
void setPassword(QTableWidgetItem *itm, const QString &password)
{
itm->setData(Qt::UserRole, password);
if (itm->data(Qt::AccessibleTextRole) == true) {
showPassword(itm);
} else {
hidePassword(itm);
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTableWidgetItem *itm = new QTableWidgetItem();
itm->setFlags(Qt::NoItemFlags);
ui->tableWidget->setItem(0, 0, itm);
setPassword(itm, "test");
connect(ui->tableWidget, &QTableWidget::itemClicked, this, [itm](QTableWidgetItem *clickedItm){
if (clickedItm == itm) {
if (itm->data(Qt::AccessibleTextRole) == true) {
hidePassword(itm);
} else {
showPassword(itm);
}
}
});
}
I also used Qt::AccessibleTextRole to store whether the password is shown or not, it seems to be free