Solved Giving file location from parent of the parent folder(Dynamically) in QT
-
@amarism said in Giving file location from parent of the parent folder(Dynamically) in QT:
@dheerendra using this code to get the file path:- QFile File(QCoreApplication::applicationDirPath() + "/LineMeas.jpg");
So since I assume your application path is not in the
icon
sub-folder, but is actuallyC:\test\project
, then obviously you need to specify:QFile File(QCoreApplication::applicationDirPath() + "/icon/LineMeas.jpg");
or whatever to address the correct desired path relative to
QCoreApplication::applicationDirPath()
. -
@JonB But how to display the file image on button
-
A QPushButton is probably not the correct widget to use, to display a dynamic icon.
However there are 2 ways I can think of
QPushButton::setIcon(QIcon(dynamicPath)); and QString btnStyle = "border-image:url(%1);" btnStyle.arg(dynamicPath); QPushButton::setStyleSheet(btnStyle);
-
@J.Hilk said in Giving file location from parent of the parent folder(Dynamically) in QT:
QPushButton::setIcon(QIcon(dynamicPath));
how to get the dynamic path
-
@amarism said in Giving file location from parent of the parent folder(Dynamically) in QT:
@J.Hilk said in Giving file location from parent of the parent folder(Dynamically) in QT:
QPushButton::setIcon(QIcon(dynamicPath));
how to get the dynamic path
?
You should read the other posts more carefully
with for example
QCoreApplication::applicationDirPath()
or
-
@amarism said in Giving file location from parent of the parent folder(Dynamically) in QT:
how to get the dynamic path
We have explained this repeatedly in the examples above.
If your dynamic path means "it's inside the Qt application's directory, in an icon sub-folder", try either plain
"icon/LineMeas.jpg"
orQCoreApplication::applicationDirPath() + "/icon/LineMeas.jpg"
.If you mean somewhere else, you'd better explain where....
-
@JonB said in Giving file location from parent of the parent folder(Dynamically) in QT:
icon/LineMeas.jpg
My icon folder place outside the QT application.
-
My icon folder place outside the QT application.
None of the posters here would have guessed that!
This does not seem like a good start for you! You sure you wouldn't be better rearranging so that it is inside the application? It would make things a lot easier....
OK, so if it is to be external: neither we nor your Qt application can possibly guess where the file(s) might be, can we? So, how do you/your users/your application actually know where this file is?? You need to specify this, else you'll never get anywhere! So what are your rules for where this file is to be found?
-
@JonB, For example, I am placing icon folder inside the QT Application but still this one not working (ui->pushButton->setIcon(QIcon(QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg"));)
-
(ui->pushButton->setIcon(QIcon(QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg"));)
But earlier on you showed the sub-folder was named
icon
, and now you show you're using nameicons
in the path... ???I'm sorry, but no-one will want to help if you cannot give a consistent, literal code example with correct paths which actually matches the paths on your disk. We don't want to spend time helping if this proves to be the problem area.
Meanwhile... you're doing too much in a single line of code to spot where the problem is. Break it down into 3 steps:
-
Use
QFileInfo
onQCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg"
or whatever, or open it for read. Until that is finding the file correctly. -
Then verify that
QIcon(QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg"))
is returning an icon, not0
or error or whatever. -
Then pass it to
ui->pushButton->setIcon()
.
One test one at a time....
-
-
@JonB said in Giving file location from parent of the parent folder(Dynamically) in QT:
ui->pushButton->setIcon()
my bad a small change in the code as of your prediction it is icons only thanks for correcting.