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



  • I have found resource file for storing images but Does we have resource file for storing widget geometry, style, color of widget.



  • Why not using QSettings?



  • [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?



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



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



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



  • [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.



  • [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.



  • [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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.