So let's say I do something like this:
QIcon myIcon(":/resources/icon.ico");
How can I later determine the path for that icon, e.g.
QString path = myIcon.getPath();
The problem is, there is no getPath() member, and I can't find anything similar, but surely there must be a way!
I guess I could inherit the QIcon class and add this functionality, but it's probably built in and I'm overlooking it?
The filename isn't stored in QIcon, it's only used for constructing the image.
Related
I would like cross-platform a way to get a system icon, for example the folder icon in the setIcon method of QWidget. Something like this:
QWidget *myWidget = new QWidget;
myWidget->setIcon(/*something to get a system icon*/);
Is this possible? If it is, how to do it?
Use QFileIconProvider::icon(IconType) to get an icon of a particular kind, from a small selection of types.
In your particular case, you'd want icon(QFileIconProvider::Folder).
Use QFileIconProvider::icon(const QFileInfo &) to get an icon for a particular directory entry.
Use QIcon::fromTheme(const QString &, const QIcon & = QIcon()) to get a theme icon on Linux/X11.
I think you're looking for QIcon::fromTheme method.
Example:
QIcon undoicon = QIcon::fromTheme("edit-undo");
Note: By default, only X11 will support themed icons. In order to use themed icons on Mac and Windows, you will have to bundle a compliant theme in one of your themeSearchPaths() and set the appropriate themeName().
For linux/X11, maybe you are looking for QMimeDatabase::mimeTypesForFileName and QIcon::fromTheme.
Refer to https://stackoverflow.com/a/45739529/5785726 for a example
I'm trying to code a C++ function to save a selected picture to my program directory, using a Qt GUI.
So far, the "save" function I'm using on my QPixmap object won't save anything, and I can't figure out why.
Here is the code :
qImage = new QPixmap(path);
QPixmap qImage2 = qImage->scaled(this->width(),this->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
qImage2.toImage();
qImage2.save(QDir::currentPath());
qDebug()<<QDir::currentPath();
Can anyone help me ? :)
QDir::currentPath() returns the current working directory. Obviously, the filename itself is not specified. Simply append the needed filename, for example: QDir::currentPath() + "/123.png"
QPixmap::toImage() is a const method returning a QImage converted from a QPixmap. It literally does nothing useful in your code, remove it or use QImage instead.
QDir::currentPath() returns the current working directory which is not necessarily an application executable directory. Use QCoreApplication::applicationDirPath() instead if you need an executable directory.
Also, as pointed out by Violet Giraffe, there could be write permission issues.
I have several Qt stylesheets that look something like this:
MyClass
{
my_color: #abcdef;
}
However, I recently moved MyClass into a new namespace, ns, which means that its corresponding qss classname is now "ns--MyClass", not just "MyClass". As a result, all my stylesheets are now invalid. Unfortunately, they aren't all under my control, so I cannot edit them manually.
So my question is: how can I configure ns::MyClass to use all the same style settings of MyClass?
Obviously, Qt has to parse the style sheet at some point. So if I could access the parsed style sheet and extract just the portions that affect MyClass, I should be able to add those portions into ns::MyClass via setStyle() or setStyleSheet(). But I cannot find a way to do this.
I am targeting Qt 4.8.
You'll need to patch Qt to add two methods to QApplication:
void setClassSubstitutions(const QMap<QString, QString> &);
QMap<QString, QString> classSubstitutions() const;
The substitutions would be held in the application's PIMPL, and you'd need to have the stylesheet mechanism use them. The entirety of the patch needed for Qt would be probably two dozen lines (or so I hope).
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.
I don't see this in the documentation anywhere, so it probably doesn't exist, but just in case:
I have a function that accepts as a parameter a vector of QImages. Each QImage was loaded from disk at some point and has not been edited--it is just read from. Ideally what I'd like to do is loop over all the QImages and output a list of their file paths to an XML file.
Unfortunately, I'm not seeing in the documentation any way to get at the original file path that the image was loaded from. So my question is, is it possible, given only a QImage, to figure out what file path the QImage was originally loaded from or not?
Something along the lines of:
QString QImage::getOriginalFilepath();
I know this is probably a futile question, but its always worth a shot to ask, I suppose.
(I'm using Qt 4.7, by the way.)
I looked through the code, and that information doesn't appear to be saved anywhere. QImage::load (and the constructor) uses QImageReader(fileName,format).read() and then copies the result to itself. The QImageReader is set to delete the device representation (the opened file object) as soon as it's finished reading.
So, in summary, it appears that this is not possible.
Also for me it seems that QImage doesn't provide an interface to get its path. I also think that this is not a missing feature, because there's no need to tie a QImage to a specific path. In the majority of all use cases the QImage has no corresponding physical file.
So the best way in your situation would be to subclass QImage and add this feature:
class MyImage : public QImage {
QString path_;
public:
MyImage(const QString& path);
QString path(); // getter
};
I omit the implementation details.
I usually create a struct for hold the QImage and the image path.
struct Image
{
QImage imageData;
QString filename;
};