Encoding in the QTableView
-
@ro12man3
ok. the first delegate can be slightly trick if u are not deeply into c++
as it involved subclassing and virtual function override.
looking at samples is a good idea. -
@mrjj I have seen the link about star delegate, but I'm not sure how to edit that for me. Can you write simple code, please?
And what to do if I have 40 codes that I will need to change?@ro12man3
hi
Delegates are a bit involved to code so small samples is not so easy.'
The main concept for you would to be override
http://doc.qt.io/qt-5.5/qstyleditemdelegate.html#displayText
so when view ask how to show data , you can then
replace the ID with the correct text.for 40 codes , you might need a lookup table as pure ifs
if ( ID == 1000) return "CITY";
will be a bit verbose :) -
Hi,
You should build a hash of your data and use it to map your numbers to your city.
-
@ro12man3
hi yes QHash would be fine
http://www.bogotobogo.com/Qt/Qt5_QHash.php -
@ro12man3
hi yes QHash would be fine
http://www.bogotobogo.com/Qt/Qt5_QHash.php -
@ro12man3
well when u create your delegate object
you also give it a Hash list with the town names.
so in displayText()
it can look up in the list the name to use from the number from DB. -
@ro12man3 said:
How to change the numbers in that variant?
Hi Im not sure what you ask about ?
QTableWidgetItem is not a variant.
-
Where's that hash located ?
Did you set your custom QStyledItem delegate on the correct column of your QTableView ?
Are you sure that the volume contains the correct values ? -
Where's that hash located ?
Did you set your custom QStyledItem delegate on the correct column of your QTableView ?
Are you sure that the volume contains the correct values ?@SGaist this is the code
void MainWindow::on_pushButton_clicked() // I click and the quiery result is showing in the qtableview { QSqlQueryModel * model = new QSqlQueryModel(0); QHash< int, QString> hash; hash.insert(100, "Jackson"); model->setQuery("select city-codes from country"); ui->tableView->setModel(model); }
-
That hash is destroyed at the end of on_pushButton_clicked
-
Either in your QStyledItemDelegate or as @mrjj suggested pass it to the delegate e.g. at construction time.
-
In your QStyledItemDelegate, read your file and build the hash with its content. Then in your displayText reimplementation
return _hash.value(value.toInt(), tr("Not found"));
-
@SGaist what will be more simple? Cause I don't know how to make that. Sorry about my not big skills in qt
@ro12man3
hi
Starting with models and view are not best intro to Qt.
They are very powerful but does demand some c++ skills and reading the docs.Sometimes code is easier to understand so I made u sample with @SGaist help.
It has a list of names and if it sees Lars, it replaced it with BANNED.
For your code, you will use the hash to look up the name from the code.https://www.dropbox.com/s/vsiqjrr41jhafqo/myfirstview.zip?dl=0
the key points to understand is:
we make a new class to override the default behaviourclass MyDelegate : public QStyledItemDelegate {
public:
MyDelegate(QObject* parent)
: QStyledItemDelegate(parent) {}
virtual QString displayText(const QVariant& value, const QLocale& locale) const;
};We set this delegate ONLY to the column we need.
- ui->tableView->setItemDelegateForColumn(0, new MyDelegate(this));
the displayText is the key function.:
QString MyDelegate::displayText(const QVariant& value, const QLocale& locale) const {
if (value.toString().compare("Lars") == 0) // if found
return "BANNED";
else
return value.toString(); // else just return data unchanged
}Here u will replace the city-code with names using the hash.
Hope this helps you.
-
@ro12man3
hi
Starting with models and view are not best intro to Qt.
They are very powerful but does demand some c++ skills and reading the docs.Sometimes code is easier to understand so I made u sample with @SGaist help.
It has a list of names and if it sees Lars, it replaced it with BANNED.
For your code, you will use the hash to look up the name from the code.https://www.dropbox.com/s/vsiqjrr41jhafqo/myfirstview.zip?dl=0
the key points to understand is:
we make a new class to override the default behaviourclass MyDelegate : public QStyledItemDelegate {
public:
MyDelegate(QObject* parent)
: QStyledItemDelegate(parent) {}
virtual QString displayText(const QVariant& value, const QLocale& locale) const;
};We set this delegate ONLY to the column we need.
- ui->tableView->setItemDelegateForColumn(0, new MyDelegate(this));
the displayText is the key function.:
QString MyDelegate::displayText(const QVariant& value, const QLocale& locale) const {
if (value.toString().compare("Lars") == 0) // if found
return "BANNED";
else
return value.toString(); // else just return data unchanged
}Here u will replace the city-code with names using the hash.
Hope this helps you.
@mrjj ohhh thank you! It works!! Thank you very much.
But I have 40 values that I will need to change(if it will be in quiery result).
- I can't write 40 "else if".. "else if"... "else if".... That's unlogic.
- I need to write all that values in support *.txt file(or another, the main is a simple format for editing), cause if I will write all that values in the program and then I will need to add new values...That's will be very uncomfortable.
I see the method of the working the function so:
The function is searching the values(from *.txt file) that has a quiery result. If it will find, then it will change to another every value.But...how make that in reality?
-
hi
it sounds like your best way would to read in
the text file to a hash.
and use that hash in displayText
I agree, using if for 40 values would be nasty :)How does your text file look like?