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. Can we make resource file in Qt from where we can read the location and many other display related things of any widget?
Forum Updated to NodeBB v4.3 + New Features

Can we make resource file in Qt from where we can read the location and many other display related things of any widget?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 4.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.
  • F Offline
    F Offline
    fluca1978
    wrote on last edited by
    #2

    Why not using QSettings?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pratik041
      wrote on last edited by
      #3

      [quote author="fluca1978" date="1322647766"]Why not using QSettings?[/quote]

      I want to store multiple widgets information in a single resource file separately . So that when i need that information related to a particular widget, i can use it directly.How we can do that by Qsetting can you explain by code in brief?

      Pratik Agrawal

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #4

        I would do:

        create a qsetting object

        create one group per widget (it could be the widget name, id, or whatever)

        create a key for the geometry and set the value

        It should be something like the following:

        @QSettings settings( "mycompany",
        "config",
        this );

        settings.beginGroup( "widget1" );                         
        settings.setValue(   "geometry",                        
                             saveGeometry() ); @
        

        iterated over each widget. A smarter and modular approach would be to have a static QSettings object that each widget uses to store its information.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #5

          Do you want to apply some default values to all objects of a QWidget subclass when they are created or do you want to save and restore the state of a specific widget?

          The former one can be achieved using resource files, the latter one cannot (resources are static, they cannot be changed at runtime). In this case use QSettings, as already mentioned by fluca.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pratik041
            wrote on last edited by
            #6

            [quote author="Lukas Geyer" date="1322650003"]Do you want to apply some default values to all objects of a QWidget subclass when they are created or do you want to save and restore the state of a specific widget?

            The former one can be achieved using resource files, the latter one cannot (resources are static, they cannot be changed at runtime). In this case use QSettings, as already mentioned by fluca.[/quote]

            I want to apply some default values to all objects of a QWidget subclass when they are created.

            Pratik Agrawal

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #7

              Why do you want those things in a resource file specifically? What do you want to achieve, what you can not achieve with using ui files in the normal way (compiled in as C++)?

              Would just keeping style sheets around be an option for you? That allows you to style your widgets to a very large extend already. You could also use [[doc:QUiLoader]], a class that allows you to load and instantiate complete .ui files. These you can store into a resource file again, but I fail to see the point to do that.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pratik041
                wrote on last edited by
                #8

                [quote author="Andre" date="1322651107"]Why do you want those things in a resource file specifically? What do you want to achieve, what you can not achieve with using ui files in the normal way (compiled in as C++)?

                Would just keeping style sheets around be an option for you? That allows you to style your widgets to a very large extend already. You could also use [[doc:QUiLoader]], a class that allows you to load and instantiate complete .ui files. These you can store into a resource file again, but I fail to see the point to do that. [/quote]

                By .ui file did you mean qt designer? I want those things in a resource file because i want to store all the default value in that and if we will store in a file separately in case of big project it will be easy to find and change any widget related properties.

                Pratik Agrawal

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #9

                  [quote author="pratik041" date="1322650351"]
                  I want to apply some default values to all objects of a QWidget subclass when they are created.
                  [/quote]

                  I would go for a stylesheet then (which can be embedded in a resource file easily), which should cover style and color. As for geometry I would create a widget factory which has the default values for all widgets.
                  @

                  // widgetfactory.h

                  class WidgetFactory
                  {
                  public:
                  QWidget* createWidget(const QString &name);

                  private:
                  struct DefaultGeometry
                  {
                  const char* name;
                  int x, y, w, h;
                  }
                  static QHash<DefaultGeometry> defaultGeometry;
                  }

                  // widgetfactory.cpp

                  QHash<DefaultGeometry> WidgetFactory::defaultGeometry =
                  {
                  // all default values go here
                  { "widgetA", 10, 10, 100, 100 },
                  { "widgetB", 10, 10, 200, 200 },
                  { "widgetC", 20, 20, 100, 100 },
                  }

                  QWidget* WidgetFactory::createWidget(const QString &name)
                  {
                  QWidget* widget = static_cast<QWidget*>(QMetaType::construct(QMetaType::type(name)));
                  DefaultGeometry geometry = defaultGeometry[name];
                  widget->setGeometry(geometry.x, geometry.y, geometry.w, geometry.h);

                  return widget;
                  

                  }

                  // main.cpp

                  int main(int argc, char argv[])
                  {
                  ...
                  MainWindow
                  mainWindow = static_cast<MainWindow*>WidgetFactory::createWidget("MainWindow");
                  mainWindow->show();
                  ...
                  }
                  @
                  Brain to terminal. Not tested. Exemplary.

                  Non top-level widgets should get their geometry from layouts.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #10

                    [quote author="pratik041" date="1322652162"]
                    [quote author="Andre" date="1322651107"]Why do you want those things in a resource file specifically? What do you want to achieve, what you can not achieve with using ui files in the normal way (compiled in as C++)?

                    Would just keeping style sheets around be an option for you? That allows you to style your widgets to a very large extend already. You could also use [[doc:QUiLoader]], a class that allows you to load and instantiate complete .ui files. These you can store into a resource file again, but I fail to see the point to do that. [/quote]

                    By .ui file did you mean qt designer?
                    [/quote]
                    Designer edits and produces these files, yes. Anything you miss in them?
                    [quote]
                    I want those things in a resource file because i want to store all the default value in that and if we will store in a file separately in case of big project it will be easy to find and change any widget related properties.
                    [/quote]
                    Sounds like Designer's ui files already do all you need. You can easily find and edit them again. They are stored as .ui files in your project, and only on building they are compiled into code. Just like happens with resource files: they are compiled into your project in a similar way. So, IMHO, you gain nothing but a whole lot of useless complexity by trying to do that all over again.

                    Also, I would advice against an approach as sketched by Lukas above. Don't try to create your own format for defining UI geometries, but use what's there already instead.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #11

                      [quote author="Andre" date="1322655755"]
                      Also, I would advice against an approach as sketched by Lukas above. Don't try to create your own format for defining UI geometries, but use what's there already instead.
                      [/quote]

                      The example was sketched under the precondition that .ui files are not applicable. If they can, .ui files are the way to go.

                      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