I am struggling with adding a custom icon to a QToolButton.
I have a resource file(properly under RESOURCES in the .pro file):
myResourceFile.qrc containing
/images
ICON_TEST.png
and code
dragButton = new QToolButton(this);
QString resourcePath = ":/images/ICON_TEST.png";
QPixmap pixIcon(resourcePath); //THE LINE THAT GIVES THE ERROR!
dragButton->setIcon(QIcon(pixIcon));
I get the error:
no match for call to QPixmap(QString &)
How do I pass the proper path to the QPixmap object and then the object to setIcon()?
edit:
Just confirmed that the .png file exists and is recognized by Qt:
File exists - true ":/images/ICON_TEST.png"
with:
qDebug()<<"File exists -"<<QFileInfo(":/images/ICON_TEST.png").exists()<<" "<<
QFileInfo(":/images/ICON_TEST.png").absoluteFilePath();
EDIT WITH SOLUTION: below as answer
Question remains, why this happened? The variable pixIcon is declared in the .h file of the class as:
QPixmap pixIcon;
and assigned a value in the constructor:
pixIcon(resourcePath);
Which to me seems to be almost equivalent to doing it in one line.
This was on Windows Vista with Qt Creator 2.0.
For some reason, when doing everything in one line it worked as it should have:
dragButton->setIcon(QIcon(QPixmap(resourcePath)));
Related
I have the following problem. I am using the Borland 2006 Compiler and I am trying to include an animation in my applicaton. First I added the TAnimate Object and then in the ObjectInspector under "FileName", every time I try to add an .avi, the Compiler says "AVI cannot be opened". Am I doing something wrong or isn't it that simple to just put a .gif or .avi into that Objectproperty?
Edit // Here ist Some CodeExample, everytime i press the button, it throws an exception and tells me that the avi File cannot be opened
void __fastcall THauptmenue_Login::Button1Click(TObject *Sender)
{
Animate1->FileName = ("C:\\Users\\Kevin\\Desktop\\C++ Gifs");
}
The FileName you showed doesn't look complete. It looks more like a path to a folder instead of a file.
In the Object Inspector, beside the FileName text box is a [...] button which brings up a file browser dialog which can add a full path and name to a file.
addendum:
If you are using the Object Inspector to pick a filename from the disk you do not need to specify a FileName property value in the code.
This overwrites any previous FileName property value.
Animate1->FileName = "C:\\Users\\Kevin\\Desktop\\C++ Gifs";
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 can't manage to view any picture on Qobjects, like a QPushButton, using the setIcon() function.
This probably has to do with the image url being erroneously specified, inside the QPixMap pix("url") function. This is what I've got:
QPixmap pix(":/Resources/flag.png");
QIcon icon(pix);
button->setIcon(icon);
I have a flag.png file inside the directory "Other files/Resources/flag.png".
I really don't know how resources should be placed.
You have to create a Qt Resource file (File > New > Qt > Resource File). Then add a prefix into the resource file, then add a file to the resource file.
After all of that is done, right click on the listing for that file and select Copy resource path. Then paste that path into your cpp file, and surround it in quotes.
Hope that helps.
just started using qt,
looked through docs, google, examples, etc.. trying to find simple examples(working mind you)
that showed how to do (imho) simple things, by themselves.
well i stumbled upon my answer and i was wondering if this approach would cause an issue later as the code becomes more complex.
there are more includes than needed for this example, but this is direct from working code.
mainwindow.h:
i added
private slots:
void vpkButton_clicked();
and after
Ui::MainWindow *ui;
i added
QLineEdit *vpkPathTxt;
in mainwindow.cpp:
after
ui->setupUi(this);
i added
connect( this->ui->vpkButton, SIGNAL( clicked() ), this, SLOT(vpkButton_clicked()) );
to connect my ui button to the proper slot, the issue was getting the string from vpkButton_clicked() to display in the line edit i made in the designer,
what ended up working for me was adding this next:
vpkPathTxt = this->ui->vpkPathTxt;
the function in my main.cpp became very easy:
(QString declarations at top outside voids)
void MainWindow::vpkButton_clicked()
{
vpkName = QFileDialog::getOpenFileName(this,
tr("Open VPK File"), "~/", tr("VPK Files (*_dir.vpk)"));
vpkPathTxt->setText(vpkName);
qDebug() << vpkName;
}
the reason i am ask is because it seems a little too easy to be reliable, and the fact that i havent seen it done like this,
any input welcome
thankyou
One problem with your slot is that you don't consider the case where the user discards the "open file" dialog. In this case, the function QFileDialog::getOpenFileName returns a null QString, so you should only proceed with your logic if the return value was not a null string:
if (!vpkName.isNull()) {
...
}
The second problem is as follows and I made some assumptions since I don't see your full code:
I guess you want to load a file using the file name the user has chosen in the dialog. But you set the file name in the line edit too, which the user can edit by hand. I also guess that the actual file loading happens in a different step (i.e. after clicking another button), so after the user has edited the file name by hand in the line edit it won't be the same than in your local variable vpkName.
When loading the file I'd read the contents of the line edit instead of the variable vpkName so the edit made by hand will be respected.
A different method is to also watch for editing of the line edit and reflect the changes in your variable too. Then it will be ok to read the variable instead of the line edit when loading the file later on.
I would like to add 3 items to statusbar. Shouldn't each be in its own label?
Is that possible? When I tried to add a second label, it gave me an error.
File mainwindow.h,
QLabel *m_statusLabel;
QLabel *m_pointLabel;
File mainwindow.cpp,
statusBar()->addWidget(m_statusLabel);
statusBar()->addWidget(m_pointLabel);
It works perfectly with one label, but when I add a second, then I get the following.
Starting C:\Users\Jansu\Desktop\cpp-praktikum05-alus\Joonistamine-build- desktop\src\bin\Joonistamine.exe...
ASSERT: "d" in file ..\..\include/QtCore/../../src/corelib/tools/qscopedpointer.h, line 112
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
C:\Users\Jansu\Desktop\cpp-praktikum05-alus\Joonistamine-build- desktop\src\bin\Joonistamine.exe exited with code -1073741819
I found the problem. I forgot to create QLabels, so I added this:
m_statusLabel = new QLabel(this);
m_pointLabel = new QLabel(this);