Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How to display / set window title full path?
QtWS25 Last Chance

How to display / set window title full path?

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
8 Posts 4 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    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);
    }

    JonBJ 1 Reply Last reply
    0
    • A Anonymous_Banned275

      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);
      }

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 from absoluteFilePath() but it will not actually be the correct one for the source file :( That is also why canonicalFilePath() 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 to gcc 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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anonymous_Banned275
        wrote on last edited by
        #3

        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.

        JonBJ 1 Reply Last reply
        0
        • A Anonymous_Banned275

          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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          J.HilkJ 1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @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.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            JonBJ KroMignonK 2 Replies Last reply
            0
            • J.HilkJ J.Hilk

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @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(?).

              1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @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.

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @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)

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                JonBJ 1 Reply Last reply
                1
                • KroMignonK KroMignon

                  @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)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @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 as 260 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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved