How to display / set window title full path?
-
Following code setWindowTitle(FILE); sets main window title to relative path.
Is there a C/C++ macro to set full path ?
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(FILE);
} -
Following code setWindowTitle(FILE); sets main window title to relative path.
Is there a C/C++ macro to set full path ?
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(FILE);
}@AnneRanch
C++ compilers (that I know of) do not provide an alternative to__FILE__
guaranteed to be an absolute path. I think you will find this to be the case for all compilers, and an alternative which is the full path is not offered.However Qt provides the QFileInfo class. I also happen to know your
__FILE__
contains leading..
s on its path (../../../something
). To get what you want, which is the resolved absolute path (i.e. deal with those..
s), aka canonical file path, I believe you can use https://doc.qt.io/qt-5/qfileinfo.html#canonicalFilePath:QFileInfo fi(__FILE__); setWindowTitle(fi.canonicalFilePath());
If that does not work and still returns a relative path let us know and we will adjust accordingly.
UPDATE
canonicalFilePath()
is not doing so well with a relative path :( https://doc.qt.io/qt-5/qfileinfo.html#absoluteFilePath seems to do the job, including resolving leading..
s, so instead use:QFileInfo fi(__FILE__); setWindowTitle(fi.absoluteFilePath());
UPDATE2
I'm afraid there is a fundamental problem here. The__FILE__
macro delivers a path relative to where the compilation/build is going on. But that is not available at run time for an application. Unless your application is run when the current working directory is the build output directory at compile time --- and I don't think it will be, even if you run from within Creator --- you will get a full path fromabsoluteFilePath()
but it will not actually be the correct one for the source file :( That is also whycanonicalFilePath()
returns an empty path.In short: I think you will find problems when trying to access a file path based on the
__FILE__
macro at runtime from your code. That macro is for use in reporting source code error messages or for accessing the file while you are in the Creator IDE editing/compiling files, there it will be correct. It is to do with what is actually passed togcc
on the command-line, discussed a bit in https://stackoverflow.com/a/30764776/489865. But when your code is running I think you will find you have trouble using it to correctly locate where that source file was at compile-time. -
Thanks very much for the reply.
Reason I asked was - I was trying to figure out why some of the "lenghty" includes fail.
Fortunately I can rearrange my files so the includes are working - if the text length is the real problem. I do not know. -
Thanks very much for the reply.
Reason I asked was - I was trying to figure out why some of the "lenghty" includes fail.
Fortunately I can rearrange my files so the includes are working - if the text length is the real problem. I do not know.@AnneRanch said in How to display / set window title full path?:
if the text length is the real problem. I do not know.
Just to say: I would doubt that is the case, whatever your issue is. I am not aware of any particular length limit on things like include paths, and I would doubt if there is some limit that it would be anywhere near wherever you are.
-
@AnneRanch said in How to display / set window title full path?:
if the text length is the real problem. I do not know.
Just to say: I would doubt that is the case, whatever your issue is. I am not aware of any particular length limit on things like include paths, and I would doubt if there is some limit that it would be anywhere near wherever you are.
@JonB actually there are some limits, those however come from the operating system.
On windows for example, the Windows API imposes a maximum filename length such that a filename, including the file path to get to the file, can't exceed 255-260 characters
also of course, special characters such as spaces can and will cause problems as well.
-
@JonB actually there are some limits, those however come from the operating system.
On windows for example, the Windows API imposes a maximum filename length such that a filename, including the file path to get to the file, can't exceed 255-260 characters
also of course, special characters such as spaces can and will cause problems as well.
@J-Hilk said in How to display / set window title full path?:
can't exceed 255-260 characters
Indeed. All OSes ultimately have some limit on file names/paths. I was trying to keep it simple: when I said "that it would be anywhere near wherever you are.": I could be mistaken but in practice I doubt @AnneRanch's include paths are anywhere near 250 characters(?).
-
@JonB actually there are some limits, those however come from the operating system.
On windows for example, the Windows API imposes a maximum filename length such that a filename, including the file path to get to the file, can't exceed 255-260 characters
also of course, special characters such as spaces can and will cause problems as well.
@J-Hilk said in How to display / set window title full path?:
On windows for example, the Windows API imposes a maximum filename length such that a filename, including the file path to get to the file, can't exceed 255-260 characters
also of course, special characters such as spaces can and will cause problems as well.It depends which Windows version you are talking about.
This limitation has been remove with Windows 10/11 (cf https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd) -
@J-Hilk said in How to display / set window title full path?:
On windows for example, the Windows API imposes a maximum filename length such that a filename, including the file path to get to the file, can't exceed 255-260 characters
also of course, special characters such as spaces can and will cause problems as well.It depends which Windows version you are talking about.
This limitation has been remove with Windows 10/11 (cf https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd)@KroMignon
True, but two things:-
You have to "opt in" for this > 260 characters by altering a Registry parameter from its default, I doubt the OP has chosen to do this.
-
Although this relaxes the limit on certain Windows OS calls, it does not mean that applications do not have code with the original 260 character limitation. It only takes a program --- or the libraries it uses --- to use
_MAX_PATH
constant, which I believe is still defined as260
or certainly used to be --- to find the program is still limited. I don't know know, but I wonder what MinGW or even MSVC are using here for their pathnames accepted from the user.
-