Giving file location from parent of the parent folder(Dynamically) in QT
-
wrote on 7 Jun 2018, 10:02 last edited by
I need to read the file location from parent folder but I don't know how.
My executed file is in
C:\test\project\icon\icon1.pngNext time the same file placed in another folder
D:\icon\icon1.pngI want to do this dynamically. So config of the icon1.png should give a path to test then into project and icon. Because this program can be located anywhere and I don't want to give a path starting from C:\
Any help would be appreciated. I'm using QT 5.10.1. Thanks
-
Don't use hard-coded path. Just check if the QCoreApplication::applicationDirPath() function help you to place the images and access the relative path.
-
wrote on 7 Jun 2018, 10:41 last edited by
Have a look at QDir header.
#include <QDir>
QDir QDir::current()
Returns the application's current directory. -
Don't use hard-coded path. Just check if the QCoreApplication::applicationDirPath() function help you to place the images and access the relative path.
wrote on 7 Jun 2018, 11:10 last edited by@dheerendra using this code to get the file path:- QFile File(QCoreApplication::applicationDirPath() + "/LineMeas.jpg");
but i am not able to display the image inside the button -
Print the QCoreApplication::applicationDirPath().
Confirm whether LineMeas.jpg is present in the above directory. If it exist, there may be a problem while setting it for button. Please check -
Print the QCoreApplication::applicationDirPath().
Confirm whether LineMeas.jpg is present in the above directory. If it exist, there may be a problem while setting it for button. Please checkwrote on 7 Jun 2018, 11:21 last edited by amarism 6 Jul 2018, 11:24@dheerendra LineMeas.jpg is present in the directory (C:\test\project\icon\LineMeas.jpg).
and i am try to use the file to display image on button :-(ui->pushButton->setWindowFilePath(File);) -
@dheerendra using this code to get the file path:- QFile File(QCoreApplication::applicationDirPath() + "/LineMeas.jpg");
but i am not able to display the image inside the buttonwrote on 7 Jun 2018, 11:52 last edited by@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()
. -
@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()
. -
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);
-
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);
wrote on 7 Jun 2018, 12:13 last edited by@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
-
@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
-
@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
wrote on 7 Jun 2018, 12:19 last edited by@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....
-
wrote on 7 Jun 2018, 12:34 last edited by
@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.
-
@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.
wrote on 7 Jun 2018, 12:50 last edited byMy 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?
-
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"));)
wrote on 7 Jun 2018, 13:13 last edited by JonB 6 Jul 2018, 14:15(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....
-
-
(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....
wrote on 7 Jun 2018, 14:03 last edited by@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.
-
1/17