QVariant to QIcon/QPixmap/QImage - c++

I want to extract a QIcon I've stored in one of a QTreeWidget's columns, as Qt::DecorationRole.
QTreeWidgetItem *item = ui->treeWidget->topLevelItem(index);
const QIcon &icon = item->data(0, Qt::DecorationRole)._howToConvert_();
However, I can only get the data as QVariant, and I could not find a function to convert from a QVariant to QIcon. Is it possible to do it?

OK, found the answer in the docs for QVariant upon further inspection.
This works:
QImage image = variant.value<QImage>();

I find the solution as follows:
QImage name_image = table_store_multi_model_->item(i_row,0)->data(Qt::DecorationRole).value().toImage();
Generally, we read data with data(), but here need a parameter "Qt::DecorationRole";

Related

How to get data in form of QString from QVariant in Qt5?

I have a QVariant with userType QVariantList and the exact format looks like this when i qDebug the qvariant
QVariant(QVariantList, (QVariant(QVariantMap, QMap(("name", QVariant(QString, "UK English Female"))))
I want QString, "UK English Female" from the variant how can i get that i have already tried QVariant.toStringList() but the stringlist is empty.
Thanks.
From what is shown as a debug output, I presume it was produced in a similar manner:
QVariantMap map;
map.insert("name", QVariant("UK English Female"));
qDebug() << QVariant(QVariantList{QVariant(map)});
and as a result you have a string data, which is burried relatively deep in a sequence of containers.
So, how to get there?
As a whole you have a QVariant holding a QVariantList with a single QVariant value, which in turn is a QMap. The map has a key name of type QVariant holding a QString. Chaining the necessary conversions, we end up with:
qDebug() << myVar.value<QVariantList>().first().toMap().value("name").toString();
assuming the whole thing is held in QVariant myVar;.

copy QPixmap of one Qlabel to another Qlabel

I am trying to add a QPixmap to a QLabel taken from another QLabel but there is an error :
Here is the code
const QPixmap *tempPix = new QPixmap("");
tempPix = (label1->pixmap());
label2->setPixmap(tempPix); //error cannot convert from const QPixmap* to const QPixmap&
and if I do it like this:
const QPixmap tempPix("");
tempPix = (label1->pixmap()); //error cannot convert QPixmap and QPixmap*
label2->setPixmap(tempPix);
To copy data from a pointer object to an object you must use the *
QPixmap tempPix;
if(label1->pixmap()){
tempPix = *label1->pixmap();
label2->setPixmap(tempPix);
}
You can write it in a single line as follows:
label2->setPixmap(*label1->pixmap());
Note that * will convert the pointer returned by pixmap() to a reference. The difference between both is explained in this thread.
Note that in your first example, the constructed QPixmap in the first line is never used and a memory leak occurs. The second line changes the pointer value, not the data of the newly constructed object.

How to get data back from QVariant for a usertype?

I'm using a QVariant to store a pointer to my object in a QComboBox
void MainFrame::initContainerBox(QComboBox *oBox)
{
IDataContainer *idc = new CSVContainer();
QVariant v(QVariant::UserType, idc);
oBox->addItem(idc->getContainername(), v);
void *idc1 = v.data();
if(idc1 == idc)
printf("Test\n");
}
But how do I get the data back? When I use data() the pointer is different, so this doesn't seem to be correct. From gooogling I had the impression that I have to register a type for each class I want to use in a QVariant is that correct or can I retrieve the value without that?
After two days of googling and trying all kind of combinations I finally found out how to do this. Here is an example using a QComboBox putting an item and getting it back. IDataContainer * is an arbitrary class which is not related to Qt.
Q_DECLARE_METATYPE(IDataContainer *)
void MainFrame::initContainerBox(QComboBox *oBox)
{
IDataContainer *idc = new CSVContainer();
QVariant v;
v.setValue(idc);
oBox->addItem(idc->getContainername(), v);
QVariant v2 = oBox->itemData(oBox->currentIndex());
IDataContainer *idc1 = v2.value<IDataContainer *>();
if(idc1 == idc)
printf("Test\n");
}
So with my first approach of using value() I was on the right track, the only missing bits were how to set the value and using the macro Q_DECLARE_METATYPE(IDataContainer *).
Apparently using the constructor doesn't work, so one has to call setValue() instead. If somebody knows how to use the constructor it would be nice to show it.

Qt ActiveX dynamicCall return value always empty

This is a follow up to a previous question: Qt ActiveX
I am trying to use an ActiveX control in my program.
QAxWidget* mAX = new QAxWidget();
mAX->setControl("{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}");
I know that there is a function like the one below (used getDocumentation()):
SendCommand(QString input, QString& output)
But when I try to execute it:
QString returString;
mAX->dynamicCall("SendCommand(QString,QString&)","something",returnString);
I always get:
returString = "";
I searched the web and saw a similar bug which was reported on their bug tracker. It does not seem fixed yet:
Calling functions through dynamicCall() don't return values by QVariant
Also a post where someone seems to have the same problem:
QAxObject and dynamicCall
Anybody know of a solution/work around ?
EDIT:
The original function is SendCommand(LPCTSTR command,BSTR* ret).
Maybe an issue with the way the BSTR* is handled as a &QString ?
you can use this solution
QString strRetVal;
QVariant returnValue("");
QVariant param1("something");
QList<QVariant> inplist;
inplist<<param1;
inplist<<returnValue;
mAX->dynamicCall("SendCommand(QString,QString&)",inplist );
strRetVal=inplist.at(1).toString();
From looking at the documentation, you are not calling the function correctly. You are passing in a QString, yet the function takes a QVariant. Since QVariant doesn't have explicit constructors (by design), a temporary QVariant is created and passed to dynamicCall. As a consequence your returnValue doesn't get updated.
QVariant dynamicCall( const char * function, const QVariant & var1 = QVariant(), ...
, const QVariant & var8 = QVariant() )
I think that everything will work when you use a QVariant instead.
QVariant returnValue;
mAX->dynamicCall("SendCommand(QString,QString&)", "something", returnValue );

Converting QModelIndex to QString

Is there a way to convert QModelIndex to QString? The main goal behind this is that I want to work with the contents of dynamically generated QListView-Items.
QFileSystemModel *foolist = new QFileSystemModel;
foolist->setRootPath(QDir::rootPath());
foolistView->setModel(foolist);
[...]
QMessageBox bar;
QString foolist_selectedtext = foolistView->selectionModel()->selectedIndexes();
bar.setText(foolist_selectedtext);
bar.exec;
Is this even the correct way to get the currently selected Item?
Thanks in advance!
foolistView->selectionModel()->selectedIndexes();
Send you back a QList of QModelIndex (only one if you view is in QAbstractItemView::SingleSelection)
The data method of QModelIndex return a QVariant corresponding to the value of this index.
You can get the string value of this QVariant by calling toString on it.
No, is the short answer. A QModelIndex is an index into a model - not the data held in the model at that index. You need to call data( const QModelIndex& index, int role = Qt::DisplayRole) const on your model with index being your QModelIndex. If you're just dealing with text the DislayRole should sufficient.
Yes the way you are getting the selected item is correct, but depending your selection mode, it may return more than one QModelIndex (in a QModelIndexList).
QModelIndex is identifier of some data structure. You should read QModelIndex documentation. There is a QVariant data(int role) method. In most cases you will need Qt::DisplayRole to get selected item text. Note that also selectIndexes() returns a list of QModelIndex. It may be empty or contain more then one item. If you want to get (i.e. comma separated) texts of all selected indexes you should do something like this:
QModelIndexList selectedIndexes = foolistView->selectionModel()->selectedIndexes();
QStringList selectedTexts;
foreach(const QModelIndex &idx, selectedIndexes)
{
selectedTexts << idx.data(Qt::DisplayRole).toString();
}
bar.setText(selectedTexts.join(", "));