是否有准备好使用对话框进行设置?

时间:2012-03-01 07:50:11

标签: c++ qt

这是一个想法:我有一个参数列表(名称,类型,值可能更多 - 输入正则表达式),按部分分组。它们存储在xml文件中(例如......可能是其他格式)。我想创建一个模块,构建依赖于此文件的“标准外观”设置对话框。像这样 settings 部分名称应该在左侧作为列表,参数,参考选定的部分,在右边:名称作为标签,值作为一些输入字段,类型取决于参数的类型(lineEdit用于文本,spinEdit - 用于数字,checkBox - 用于布尔值等) 最后一个问题:我的目的是否有“随时可用”的对话框? 谢谢。

2 个答案:

答案 0 :(得分:0)

没有这样的对话。

如何手动创建“完整”对话框,然后禁用/隐藏在特定情况下未使用的元素?

答案 1 :(得分:0)

我搜索了一段时间,没有找到任何决定。这就是我决定自己写的原因。 这里有一点描述:

1主要概念

如果您的程序有一些参数,您希望从GUI中编辑,您应该以正确的格式编写xml文件,创建类Config的实例,调用插槽Config :: show()并最后请求必要的参数通过Config :: get()

2类Config的用法

您可以随意创建它。您可以在c-tor或Config :: load()方法中提供xml文件名/路径。您可以在程序的任何位置获取任何参数 调用Config :: get()方法。类Config的用法示例:

Config config( "full/path/to/settings.xml" );
connect( settingsAction, SIGNAL( trigger() ), &config, SLOT( show() ) );
... 
if ( config.get( "section_name", "param_name" ).toBool() ) {
    ...
}
QFile file( config.get( "section_name", "directory_for_log" ).toString() +
    "/prog.log" );

3限制

配置需要QtCore,QtGui和QtXml。它使用C ++ 11的功能。

4 Xml文件格式

很抱歉,但是我懒得写完整的说明,所以我会写一个包含所有功能的完整示例。我想,你会理解所有这些:)

<?xml version="1.0" encoding="System"?>
<config>
    <section visible="true" name="First section name">
        <group visible="true" checkable="true" checked="true" name="First group">
            <value visible="true" type="text" value="192.168.1.1:1234" regexp="ip-addr:port" name="Ip address:port"/>
            <value visible="true" type="bool" value="false" name="Some bool value"/>
            <value visible="true" type="combo" value="Just three" items="The one;The two;Just three" name="Choose 1"/>
            <value visible="true" type="file" value="/etc/some/file.jpg" name="Picture or sound" filters="Images (*.png *.jpg);;Sounds (*.mp3 *.wav)"/>
            <value visible="true" type="radio" value="Fourth" items="First;Second;Third;Fourth" name="Choose 2"/>
            <value visible="true" type="dir" value="/etc" name="Dir for log"/>
        </group>
        <group visible="true" checkable="false" checked="true" name="Second group">
            <value unit=" cm" visible="true" type="int" value="18" min="1" name="Length" max="33"/>
            <value unit=" kg" visible="true" type="int" value="42" min="0" name="Weight" max="100"/>
        </group>
        <value visible="true" type="bool" value="true" name="Just bool"/>
    </section>
    <section visible="true" name="Second sect">
        <value visible="true" type="text" value="hello" regexp="(hello)+" name="Greet me"/>
        <value visible="false" type="bool" value="true" name="Invisible bool"/>
    </section>
</config>

这是一张照片 enter image description here

以下是Config与此文件的用法

std::ostream & operator<< ( std::ostream & os, const QVariant & var ) {
    if ( var.type() == QVariant::Bool ) {
        os << var.toBool();
    }
    else if ( var.type() == QVariant::Int ) {
        os << var.toInt();
    }
    else if ( var.type() == QVariant::String ) {
        os << qPrintable( var.toString() );
    }
    return os; }

cout << "First group checked : " << cfg.get( "First section name", "First group" ) << endl;
cout << "Ip address:port : " << cfg.get( "First section name", "Ip address:port" ) << endl;
cout << "Some bool value : " << cfg.get( "First section name", "Some bool value" ) << endl;
cout << "Choose 1 : " << cfg.get( "First section name", "Choose 1" ) << endl;
cout << "Picture or sound : " << cfg.get( "First section name", "Picture or sound" ) << endl;
cout << "Choose 2 : " << cfg.get( "First section name", "Choose 2" ) << endl;
cout << "Dir for log : " << cfg.get( "First section name", "Dir for log" ) << endl;
cout << "Length : " << cfg.get( "First section name", "Length" ) << endl;
cout << "Weight : " << cfg.get( "First section name", "Weight" ) << endl;
cout << "Just bool : " << cfg.get( "First section name", "Just bool" ) << endl;
cout << "Greet me : " << cfg.get( "Second sect", "Greet me" ) << endl;
cout << "Invisible bool : " << cfg.get( "Second sect", "Invisible bool" ) << endl;

最后,你能告诉我发布源代码的最佳方法吗? 谢谢。

UPD:您可以在此处找到https://sourceforge.net/projects/guisettings/

相关问题