what qwidget subclass?
-
i need a widget that will have borders and an icon inside it (in the center of the widget), so i decided to use QFrame since it has a QFrame::Box property, and i'm drawing a pixmap on it but something is wrong. here's my code:
class myWidget : public QFrame { Q_OBJECT public: myWidget(const QString &iconPath, QWidget *parent = nullptr); virtual ~myWidget() override {} protected: virtual void paintEvent(QPaintEvent *event) override; private: QPixmap pixmap; };
implementation:
myWidget::myWidget(const QString &iconPath, QWidget *parent /* = nullptr */) : QFrame(parent) { setAttribute(Qt::WA_TranslucentBackground); setFixedSize(140, 180); setFrameStyle(QFrame::Box); pixmap.load(iconPath); } void myWidget::paintEvent(QPaintEvent */* event */) { QPainter painter(this); painter.drawPixmap(0, 0, width(), height() / 2, pixmap); }
not only i don't get borders drawn around the widget, but also no icon loaded (actually, when i remove the paintEvent(), the border is drawn (because of QFrame::Box) but not with it).
this is how i use the class:
auto layout = new QVBoxLayout; layout->addWidget(new myWidget(icon-full-path, this)); // etc...
the icon-full-path contains "\\"s instead of "\"s, just in case, but no icon is displayed
i've also tried using all "/"s but no success
please help!
-
Hi
You must call the base class's paintEvent so it can draw the bordervoid myWidget::paintEvent(QPaintEvent event ) {
QFrame::paintEvent(event); // let base class paint
....About the icon.
Are you use external file ?
Try with resource file and that syntax
Like ":/SMSicons/foo.png"
http://doc.qt.io/qt-5/resources.html -
but QPixmap can load from QString only, that's why i changed it from QIcon.
and yes, i'll need to load external files too so that's another problem
-
@user4592357
Hi
If you need to load external files, where will they be placed ?
The user will point to them ?Using a Resources file, includes the images in the exe file and can be loaded with QString and pixmap.
-
the user will provide a path to the image, and i should go get the image from the path and display it in the widget
EDIT: i was able to draw using an entry from qrc but what should i do with that user provided icon paths?
-
Ok, sounds like a normal file open dialog
http://www.codebind.com/cpp-tutorial/qt-tutorial/qt-tutorials-beginners-qfiledialog-getopenfilename-example/Then load it via pixmap. Should just work.
-
@user4592357
Ok, i see no reason it should not work if the paths are fully qualified and
valid.I would test that
qDebug() << "loading: " pixmap.load(iconPath);
says true
also i would try with#include <QFileInfo> bool fileExists(QString path) { QFileInfo check_file(path); // check if file exists and if yes: Is it really a file and no directory? return check_file.exists() && check_file.isFile(); } and check iconPath where u try to load the image.
Also where does icon-full-path come from ?
Is it in the app or do you load from text file ? -
thanks. i'll read a text file for that, but why does that matter?
-
@user4592357
Well if inside the app you must escapec:\\xxx\\cyy\\
But that is not valid if from text file so just asking to try to guess reason that is not shown :)
-
QFile::exists() gives false when i use a full path for the image (like, C:\Users\user\image.png). but the file sure exists. i've tried using \\ instead of \, also / but not much success. with resource system, however, everything is okay
-
@user4592357
if you take one of the paths from the file
and start paintbrush, select file->open
and paste the path into the filename area. and then press open.
will it then open ?it sounds really, really wrong if QFile::exists() says false.
-
Hi
Did you try to qDebug() << file.filePath();
to see if anything still 100% like the one in file? -
@user4592357
Really cannot guess then.
If path in text file truely are valid syntax, it should just work.
and you are 100% sure you escape it ?
( which is needed when inside the code, but from text file)setIcon("C:\\Users\\user\\image.png");
-
@user4592357
I have done it many times so I do
wonder what you do that is
different or if it really just is a path issue. -
Hi,
Since you are using Qt, why not use the Unix notation ? Qt handles the conversion when it calls the related platform specific functions.
So it should be
C:/Users/user/image.png