QVariant conversion to QPainterPath - c++

I have a problem right now with my mini-game I am making. The problem is as follows: I have created an level editor for my game and thus I had to create my own delegate and model, the problem occurs when I try to edit through a shapeeditor ( which more likely creates a painterpath ). I then return the painterpath through data but when I try to paint it with my delegate, qt tells me the following error:
/usr/include/qt4/QtCore/qmetatype.h:169: error: 'qt_metatype_id' is not a member of 'QMetaTypeId<QPainterPath>'
I am not quite sure why I am having this error. For information regarding the source code of the project, I can give if needed. But I am simply thinking the conversion from qvariant to qpainterpath isn't possible. They must be a way to do it.
Note: I tried to do the following
QVariant var = index.model()->data(index, Qt::DecorationRole);
QPainterPath path = var.value<QPainterPath>(); // The error occurs here, this is line 169
But this didn't work >.< Thanks if you can help me
Possible solution, is there anyway to create a pixmap from the painterpath? I could simply return the pixmap instead of the painterpath.

Looks like you need to use Q_DECLARE_METATYPE macro with QPainterPath
Like
Q_DECLARE_METATYPE (QPainterPath)
Here is documentation for the same.

Related

How do I cast QML items to corresponding C++ Item in Qt Quick

My question is best clarified by an example. I have QML with a Text{} item. In C++ I can get to this item and I have no problem using qobject_cast to turn anything into a QQuickItem*. But how do I turn it into the closest corresponding item so that I can call more specific methods directly like setText() the same way I might call setWidth()? I realize I can use the generic setProperty() method but I'm after the compile time checking that casting offers.
I'm after a more general answer for finding the correspondence between QML and their C++ classes, so that I can find out how to do this for Rectangles, MenuBars etc. I can't seem to find this in the docs. For those that prefer code examples:
auto text_object = app_item->findChild<QObject*>("myTextArea");
text_object->setProperty("text","New Text set from Code"); //THIS WORKS BUT...
auto text_qitem = qobject_cast<QQuickItem*>(text_object);
text_qitem->setWidth(128);
auto text_quick_text = qobject_cast<WHATGOESHERE???*>(text_object);
text_quick_text->setText("new Text for qml item"); //I WANT TO DO THIS
Q: but I'm after the compile time checking that casting offers.
qobject_cast does not offer any compilation-time checking. It is all runtime and dynamic, thus this request is not plausible. The context property is fine, or you could also get the class name with QMetaObject. Then, you could build a static LUT, but the effort may not be worth it overall...
All QML properties and methods are exposed to the meta-object system and can be called from C++ using Object::setProperty and QMetaObject::invokeMethod() respectively. invokeMethod parameters and return values passed from QML are always translated into QVariant values in C++:
QString msg("That's it");
auto text_object = app_item->findChild<QObject*>("myTextArea");
if (text_object)
QMetaObject::invokeMethod(text_object, "append", Q_ARG(QString, msg));

Cannot set width of QLabel at runtime

I am doing this
ui.label->geometry().setWidth(12);
However the error I get is
Error 1 error C2662: 'QRect::setWidth' : cannot convert 'this' pointer from 'const QRect' to 'QRect &'
Any suggestions on how to resolve this ?
Geometry returns a const QRect reference, so you need to use it like this:
QRect r = ui.label->geometry();
r.setWidth(12);
ui.label->setGeometry(r);
Or you can use resize:
ui.label->resize(12, ui.label->height());
But you can also tell us what are you trying to accomplish and maybe we can find a solution that puts the QLabel into a layout and you won't need to manually resize it.
geometry() returns you const reference to QRect so it can only be used as read-only information.
Not very beautiful way but you may try calling setMinimumWidth(), setMaximumWidth() functions with the same desired value as an argument.
Actually resizing label despite it's contents is very suspicious operation)

QT - How to retrieve QVariant Values from combobox?

I'm using QVariant to store a object inside of a Qcombobox, This appears to work fine. This is the implementing code:
Add type to QVariant in header:
Q_DECLARE_METATYPE(CDiscRecorder*)
pDiscRecorder Casted as CDiscRecorder:
CDiscRecorder* pDiscRecorder = new CDiscRecorder();
Then stored in the combobox
ui->cbDrives->addItem(QString::fromWCharArray(strName), QVariant::fromValue(pDiscRecorder));
The problem arises when I try to pull it out:
CDiscRecorder* discRecorder = this->ui->cbDrives->itemData(index).value<CDiscRecorder*>;
I receive the error:
error C3867: 'QVariant::value': function call missing argument list; use '&QVariant::value' to create a pointer to member
I tried to implement the hint in the error code to no avail, I have followed the thread Add QObject in the combo box of Qt to implement this behavior, how can get my object back ?
Thanks
The compiler is giving you the hint that the argument list is missing - all you should need to do is add the brackets to tell it that you're trying to call the function. So change it to
CDiscRecorder* discRecorder = this->ui->cbDrives->itemData(index).value<CDiscRecorder*>();
And it should work. That's quite a long line, might be cleaner to break it out
QVariant variant = this->ui->cbDrives->itemData(index);
CDiscRecorder* discRecorder = variant.value<CDiscRecorder*>();

cant get a pointer to wxwidget object, using wxsmith

straight to the point:
Im learning wxsmith and wxwidgets toolkit, i created some basic GUI containing one button and 2 static text fields. GUI is compilling ok so far. My frame name is proba2Frame, then im adding my own function which is not a member of any class but i declared in header file for proba2Frame that my function is a friend. Below is code of my function:
wxStaticText * dawajpointera()
{
wxStaticText * text;
text = proba2Frame.wxStaticText.StaticText1;
return text;
}
im getting error:
expected primary-expression before ‘.’ token
What exactly im doing wrong and how to get a pointer StaticText in case my solution is completely wrong ?
Thank You in advance
You make it sound like proba2Frame is the name of a class inheriting wxFrame?
If so, you're haveing problems because you haven't created an instance of proba2Frame, and you're trying to access a part of it that hasn't been constructed. Your main frame class is simply a template for your GUI, not the GUI itself.
The best way to go about it would probably be to take an instance of proba2Frame as a parameter-
wxStaticText* dawajpointera(proba2Frame *frame)
{
return frame->StaticText1;
}
Of course, that function itself was a bit pointless, but I'll assume that you're going to do something more involved with the pointer afterwards, and want it set to a pointer named text within the function for the sake of brevity.
void func(proba2Frame *frame)
{
wxStaticText *text = frame->StaticText1;
// Do something with text
}
If you're doing this, though, please consider making the function a method of proba2Frame.
wxStaticText is the name of a wxWidgets class. You should not be naming attributes of your frame 'wxStaticText'. Despite the code you have posted, I doubt that you have really done such a terrible thing. What you probably meant to write, I would guess, is:
text = proba2Frame.StaticText1;
I am guessing that the name of the attribute is StaticText1, a pointer to an instance of the wxStaticText class.

Standard and "exotic" icons

I'm trying to use the standard icons in Qt for a QToolButton but I have a problem. My code is:
m_buttonZoomPlus->setIcon(QStyle::standardIcon(QStyle::SP_DesktopIcon));
I get the error message :
cannot call member function 'QIcon QStyle::standardIcon(QStyle::StandardPixmap, const QStyleOption*, const QWidget*) const' without object
What does it mean? Do I Have to create an empty QStyle object and call the standardIcon function on it?
Besides, I found a list of standard icons here: http://doc.trolltech.com/main-snapshot/qstyle.html#StandardPixmap-enum
Is this list exhaustive or are there other standard icons? I'm looking for instance for a zoom-in/out icon and I've not yet been able to find it.
Thank you very much for you help.
It means standardIcon is not a static method so you can't call it that way. You need to construct a QStyle and initialize it appropriately then you can use that method to get a specific icon.
Edit: Jeremy is right. If you aren't changing the style or defining your own style you can simply use the following:
QApplication::style()->standardIcon(QStyle::SP_DesktopIcon);
Reference: http://doc.qt.io/qt-5/qstyle.html#standardIcon