Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing unique_ptr for Qt methods
Forum Updated to NodeBB v4.3 + New Features

Passing unique_ptr for Qt methods

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.6k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    Exotic_Devel
    wrote on last edited by
    #1

    I'm making use of unique_ptr in the code, but when I try to pass this pointer to a method Qt, the complilator complains about the conversion and does not recognize the method called passing a unique_ptr.

    @
    unique_ptr<QUiLoader> loader ( new QUiLoader());
    unique_ptr<QFile> ui_file (new QFile("Forms/MainWindow.ui"));
    ui_file->open(QFile::ReadOnly);
    return loader->load(ui_file, parent);@

    output error
    _
    error: no matching function for call to ‘QUiLoader::load(std::unique_ptr<QFile>&, QWidget*&)’
    return loader->load(ui_file, parent);
    note: no known conversion for argument 1 from ‘std::unique_ptr<QFile>’ to ‘QIODevice*’_

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Exotic_Devel
      wrote on last edited by
      #2

      For this I need to use the function get () of unique_ptr

      @
      unique_ptr<QUiLoader> loader ( new QUiLoader());
      unique_ptr<QFile> ui_file (new QFile("Forms/MainWindow.ui"));
      ui_file->open(QFile::ReadOnly);
      return loader.get()->load(ui_file.get(), parent);
      @

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Just a couple of notes.
        unique_ptr has overloaded operator-> so instead of
        @
        loader.get()->load(...
        @
        you can just write
        @
        loader->load(...
        @
        Another thing is that there's no real benefit in dynamically allocating these objects and using unique_ptr here. Actually it's worse because you need to create unique_ptr instance and reference objects indirectly.
        This essentially does the same, just faster and easier to read:
        @
        QUiLoader loader;
        QFile ui_file("Forms/MainWindow.ui");
        ui_file.open(QFile::ReadOnly);
        return loader.load(&ui_file, parent);
        @

        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