Thomas Andrees
Thomas Andrees

Reputation: 251

Hide text in column as password dots in QtTableWidget

I have table (created by QTableWidget).

My table has 3 columns

# | user | pass

Text in user and in password is visible i.e "user", "password"

I want to hide text in pass like:

"********" < means "password"

In QLineEdit is good option called "echomode" but it is only for QLineEdit.

I can manually replace text for *, but how can i read this text later from a table (in class) ?

Better than ** will be dots. (like echomode -> password)

Regards

Upvotes: 1

Views: 2044

Answers (3)

Chris
Chris

Reputation: 348

It's a bit old but I would like to warm it up, because it wasn't working that easily for me with Qt5.

The proper way to do this is to use a QStyledItemDelegate and override the paint method. But it seems so, that you've to paint on the widget stored in the option view (I looked into Qt's source).

QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
opt.widget->style()->drawItemText(painter, opt.rect, Qt::AlignLeft, opt.palette, true, "*****");

When I do not do it in this way I get a lot of strange side effects when table cell's selection changes.

Upvotes: 0

vahancho
vahancho

Reputation: 21220

I would set the table item text to "*****", and store the real password as an item data with specific role. For example:

// Get the password item of first row
QTableWidgetItem *passwordItem = tableWidget->item(0, 2);
passwordItem->setText("*****");
passwordItem->setData(Qt::UserRole, "the_actual_password");

Extracting the actual password could be made in the similar way:

QString actualPassword = passwordItem->data(Qt::UserRole).toString();

Upvotes: 1

dhganey
dhganey

Reputation: 113

You create a QStyledItemDelegate that you set on the column of passwords in the view (not the model, setItemDelegateForColumn()). When asked to create the editor (createEditor()) it should create a QLineEdit set to obscure the echo. You can have the delegate look at the value in another column before deciding whether to obscure the password.

http://www.qtcentre.org/threads/55315-How-can-i-have-echomode-in-QtableView-for-password-column

Upvotes: 0

Related Questions