Hide text in column as password dots in QtTableWidget - c++

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

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();

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

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.

Related

C++ Builder Convert TIBQuery to TIBTable

How to convert TIBQuery to TIBTable, additionally to display it in DBGrib?
I want to sort data in DBGrid and I used TIBQuery to take sort data from database, and I have problem to convert data from TIBQuery to TIBTable.
Create a VCL C++Builder app - Drop in TIBDatabase, TIBTable, TDataSource and TDBGrid on your form. Make active the TIBDatabase and TIBTable connections. Right mouse click on the TIBTable to add the columns you want to have appear in your TDBGrid. Leave the Column Header text to be the same as the Column Names.
Add the following line of code to the onTitleClick for the DBGrid:
void __fastcall TForm1::DBGrid1TitleClick(TColumn *Column)
{
// set TIBTable's IndexFieldNames property to the column title field name
// this will sort ascending all of the data in the DBGrid
IBTable1->IndexFieldNames = Column->FieldName;
}
If you want to do even more, I suggest that you add a TDataSetProvider and TClientDataSet to your form and then you can do more with your app.

Add QTableView to QComboBox

I'm retrieving a set of results from the database and I want to populate the QComboBox with the resulting columns from the database (each row of the QComboBox should have the same columns as database result) and after that I would like to be able to retrieve from one row of the QComboBox a specific column and use it further in the app. I'm thinking if it would be possible to add the QTableView to QComboBox. I'm want to do this because I want to add more meaning to the results in a way that some result columns are just plain numbers and other are the description information.
I found out that it would be possible to concatenate the result and populate the QComboBox but this will leave me with only one value for each row to work with and I have to explode the string to obtain the exact part that it is needed to work with.
The popup that comes by default is a QListView, this can be changed with any object that inherits from QAbstractItemView, and in this case a QTableView will be used for it to use the setView() method, the result when clicking only should return a item of the selected row, then to set the column to be displayed after being selected will use the method setModelColumn() indicating the position of the column, but before that the model is set to the QComboBox using the method setModel().
# my model
model = new QSqlTableModel;
model->setTable("person");
model->select();
# setModel
comboBox->setModel(model);
# select column
comboBox->setModelColumn(1);
QTableView *view = new QTableView(this);
comboBox->setView(view);
Note: The model is set to QComboBox, not to QTableView. Also you could have problems with the width of QTableView, so we must resize, in my case use the following:
view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
view->setMinimumWidth(500);
The complete example can be found in the following link

QDate empty value on add product page

I have a form to insert a product on vector. My form is a QT dialog form and I'd like that the space where I insert the date of purchase is blank and when I click on the QDateEdit the current date appears and I can set the date I prefer.
When I add the date to the vector ( both blank and setted date) I show it on QTableWidget. the column of purchase date must show me that value and if it is blank I'd like to be able to set the date I prefer ( after this I have a function to update the info on the vector).
How can I do this? Because on Qdate Class I have nothing that allows me to do this thing (http://doc.qt.io/qt-5/qdate.html).
I have to use qt and c++
thank you, I hope I explained the problem in a good way.
An easy way: use a customized QDateEdit and enable the QCalendarWidget popup, you can customize it using QSS. Example:
When the user sets the new date, just connect the signal dateChanged() whit your own slot and update your data.

Initialize record on cell enter of XamDataGrid

Assume we have a XamDataGrid with 10 columns.
Column 1 is a XamComboEditor bound to a collection in the model
This can't be changed, the data is coming from a server and the combo's collection is based on different selections within the model so it's very dynamic.
Columns 2 - 10 are just normal alpha numeric fields
The problem:
When you enter a alpha numeric and start typing the model is initialized and everything is fine. However, if you go to the very last row, the non-initialized empty one, and click on the combo editor before entering any data into any of the other fields, the combo editor is empty.
Now I am well aware of why this is happening, it's clear that this is due to the model not being initialized yet. I'm just not sure the best method to fix this.
I would hope there is a property on the XamDataGrid that adjusts when the record is initialized but I've searched the Infragistics documentation and examples and I can't find anything.
There are events for:
EditModeStarting
EditModeStarted
EditModeEnding
EditModeEnded
private void OnCellEditModeStarting(object sender, EditModeStartingEventArgs args)
{
if (args.Cell.Field.Name == "TotalQuantity")
{
DataRecord record = args.Cell.Record;
if (record == null)
return;
MyGridEntry item = record.DataItem as MyGridEntry;
// Do a thing
}
}
You can also respond to the InitializeRecord event. It can fire for multiple reasons, such as cell editing, so check the state of your row model when responding to it. All these events are on the parent grid, not any FieldLayouts or Fields.
<i:XamDataGrid x:Name="myGrid"
InitializeRecord ="OnInitializeRecord"
EditModeStarting ="OnEditModeStarting">

QSqlTableModel primary key to row

I have a QSqlTableModel and I want to insert and update records in it with a special form in a child-window. It's a design choice not to allow "inline editing" which I disabled on purpose.
When the user selects an entry (which can be sorted and filtered through a QSortFilterProxyModel and is presented through a QTableView), he has three options (represented by buttons): delete, edit and add.
My problem is with editing:
The parent-widget emits a signal with a given record and executes a model child-view
The child-widget prepares a form based on the record, waits for user input, validates it, creates a record and sends it back to the parent-widget.
The parent-widget takes the record and puts it into the database.
And right here is the problem! One can get the right record by row quite easily, like so:
void Parent::on_button_edit_record_clicked()
{
// Table could be sorted/filtered!
row = proxyModel->mapToSource(ui->tableView->currentIndex()).row();
QSqlRecord r = model->record(row);
emit editRecordSignal(record);
child->exec();
}
void Parent::editRecord(const QSqlRecord &record)
{
model->setRecord(row, record);
}
As you can see, I manually save the row of the record I want to update. I don't think this is a nice way to handle this. Actually it seems rather hacky to me.
What I missed was an easy way to translate a primary key to a row and vice versa. Like:
void Parent::editRecord(const QSqlRecord &record)
{
model->setRecord(model->primaryKeyToRow(record->value("id")), record);
}
Is there any way to easily do this (without having to extend the QSqlTableModel), so did I miss something or do I really need to save the row manually to achieve what I want?