Qt编程之实现属性窗口编辑器

类似于这种:

就是右下角这个框,有属性名字和对应的value编辑。

这个Widget是作为一个QDockWidget被添加到QMainWindow中的。QMainWindow必须要有centralWidget才能添加DockWidget。DockWidget是可以浮动于centralWidget周边(上下左右)的一种Widget。QMainWindow中有addDockWiget这相应的函数添加。

而QDockWidget中可以设置相应的Widget,这个Widget(实际上就是属性编辑浏览窗口。)这个Widget实际上不是Qt Native自带的。而是Qt的解决方案中提供的Widget,也就是别人帮你封装做好了Widget,拿来用就可以了。这个Widget就是QtPropertyBrowser

样例代码:

nodeproperty.h  此类继承了QDockWidget

 1 #ifndef NODE_PROPERTY_H
 2 #define NODE_PROPERTY_H
 3
 4 #include <QObject>
 5 #include <QDockWidget>
 6 #include "diagramitem.h"
 7
 8
 9 QT_BEGIN_NAMESPACE
10
11 class QDockWidget;
12 class QtVariantPropertyManager;
13 class QtProperty;
14 class QtVariantProperty;
15 class QtVariantEditorFactory;
16 class QtTreePropertyBrowser;
17 QT_END_NAMESPACE
18
19
20 class NodePropertyWidget:public  QDockWidget
21 {
22 public:
23
24     NodePropertyWidget(DiagramItem::DiagramType type, QWidget* parent = 0);
25     ~NodePropertyWidget();
26
27 private:
28     void createProcessTreeProperty();
29     void createDisplayTreeProperty();
30     void createArrowTreeProperty();
31
32 private:
33     QtVariantPropertyManager *variantManager;
34     QtProperty *topItem;
35     QtVariantProperty *item;
36     QtVariantEditorFactory *variantFactory;
37     QtTreePropertyBrowser *variantEditor;
38 };
39
40
41
42
43 #endif

nodeproperty.cpp

  1 #include <QDate>
  2 #include <QLocale>
  3 #include <QDockWidget>
  4 #include <QSizePolicy>
  5
  6 #include "qtpropertymanager.h"
  7 #include "qtvariantproperty.h"
  8 #include "qttreepropertybrowser.h"
  9
 10 #include "nodeproperty.h"
 11
 12
 13
 14
 15
 16 NodePropertyWidget::NodePropertyWidget(DiagramItem::DiagramType type, QWidget * parent) :QDockWidget(parent)
 17 {
 18
 19     switch (type)
 20     {
 21     case DiagramItem::Process:
 22         setWindowTitle("Process Property");
 23         setFloating(true);
 24
 25         setAllowedAreas(Qt::RightDockWidgetArea);
 26         setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Preferred);
 27         createProcessTreeProperty();
 28         break;
 29     case DiagramItem::Display:
 30         setWindowTitle("Display Property");
 31         setFloating(true);
 32
 33         setAllowedAreas(Qt::RightDockWidgetArea);
 34         setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Preferred);
 35         createDisplayTreeProperty();
 36         break;
 37     case DiagramItem::Line:
 38         setWindowTitle("Arrow Property");
 39         setFloating(true);
 40
 41         setAllowedAreas(Qt::RightDockWidgetArea);
 42         setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Preferred);
 43         createArrowTreeProperty();
 44         break;
 45     default:
 46         break;
 47     }
 48 }
 49
 50 NodePropertyWidget::~NodePropertyWidget()
 51 {
 52
 53     delete variantManager;
 54     delete variantFactory;
 55     delete variantEditor;
 56 }
 57
 58
 59 void NodePropertyWidget::createProcessTreeProperty(){
 60
 61     variantManager = new QtVariantPropertyManager();
 62
 63     int i = 0;
 64     topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),
 65         QString::number(i++) + QLatin1String("Process Property"));
 66
 67     item = variantManager->addProperty(QVariant::Bool, QString::number(i++) + QLatin1String(" Bool Property"));
 68     item->setValue(true);
 69     topItem->addSubProperty(item);
 70
 71     item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property"));
 72     item->setValue(20);
 73     item->setAttribute(QLatin1String("minimum"), 0);
 74     item->setAttribute(QLatin1String("maximum"), 100);
 75     item->setAttribute(QLatin1String("singleStep"), 10);
 76     topItem->addSubProperty(item);
 77
 78     item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property (ReadOnly)"));
 79     item->setValue(20);
 80     item->setAttribute(QLatin1String("minimum"), 0);
 81     item->setAttribute(QLatin1String("maximum"), 100);
 82     item->setAttribute(QLatin1String("singleStep"), 10);
 83     item->setAttribute(QLatin1String("readOnly"), true);
 84     topItem->addSubProperty(item);
 85
 86     item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property"));
 87     item->setValue(1.2345);
 88     item->setAttribute(QLatin1String("singleStep"), 0.1);
 89     item->setAttribute(QLatin1String("decimals"), 3);
 90     topItem->addSubProperty(item);
 91
 92     item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property (ReadOnly)"));
 93     item->setValue(1.23456);
 94     item->setAttribute(QLatin1String("singleStep"), 0.1);
 95     item->setAttribute(QLatin1String("decimals"), 5);
 96     item->setAttribute(QLatin1String("readOnly"), true);
 97     topItem->addSubProperty(item);
 98
 99     item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property"));
100     item->setValue("Value");
101     topItem->addSubProperty(item);
102
103     item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (Password)"));
104     item->setAttribute(QLatin1String("echoMode"), QLineEdit::Password);
105     item->setValue("Password");
106     topItem->addSubProperty(item);
107
108     // Readonly String Property
109     item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (ReadOnly)"));
110     item->setAttribute(QLatin1String("readOnly"), true);
111     item->setValue("readonly text");
112     topItem->addSubProperty(item);
113
114     item = variantManager->addProperty(QVariant::Date, QString::number(i++) + QLatin1String(" Date Property"));
115     item->setValue(QDate::currentDate().addDays(2));
116     topItem->addSubProperty(item);
117
118     item = variantManager->addProperty(QVariant::Time, QString::number(i++) + QLatin1String(" Time Property"));
119     item->setValue(QTime::currentTime());
120     topItem->addSubProperty(item);
121
122     item = variantManager->addProperty(QVariant::DateTime, QString::number(i++) + QLatin1String(" DateTime Property"));
123     item->setValue(QDateTime::currentDateTime());
124     topItem->addSubProperty(item);
125
126     item = variantManager->addProperty(QVariant::KeySequence, QString::number(i++) + QLatin1String(" KeySequence Property"));
127     item->setValue(QKeySequence(Qt::ControlModifier | Qt::Key_Q));
128     topItem->addSubProperty(item);
129
130     item = variantManager->addProperty(QVariant::Char, QString::number(i++) + QLatin1String(" Char Property"));
131     item->setValue(QChar(386));
132     topItem->addSubProperty(item);
133
134     item = variantManager->addProperty(QVariant::Locale, QString::number(i++) + QLatin1String(" Locale Property"));
135     item->setValue(QLocale(QLocale::Polish, QLocale::Poland));
136     topItem->addSubProperty(item);
137
138     item = variantManager->addProperty(QVariant::Point, QString::number(i++) + QLatin1String(" Point Property"));
139     item->setValue(QPoint(10, 10));
140     topItem->addSubProperty(item);
141
142     item = variantManager->addProperty(QVariant::PointF, QString::number(i++) + QLatin1String(" PointF Property"));
143     item->setValue(QPointF(1.2345, -1.23451));
144     item->setAttribute(QLatin1String("decimals"), 3);
145     topItem->addSubProperty(item);
146
147     item = variantManager->addProperty(QVariant::Size, QString::number(i++) + QLatin1String(" Size Property"));
148     item->setValue(QSize(20, 20));
149     item->setAttribute(QLatin1String("minimum"), QSize(10, 10));
150     item->setAttribute(QLatin1String("maximum"), QSize(30, 30));
151     topItem->addSubProperty(item);
152
153     item = variantManager->addProperty(QVariant::SizeF, QString::number(i++) + QLatin1String(" SizeF Property"));
154     item->setValue(QSizeF(1.2345, 1.2345));
155     item->setAttribute(QLatin1String("decimals"), 3);
156     item->setAttribute(QLatin1String("minimum"), QSizeF(0.12, 0.34));
157     item->setAttribute(QLatin1String("maximum"), QSizeF(20.56, 20.78));
158     topItem->addSubProperty(item);
159
160     item = variantManager->addProperty(QVariant::Rect, QString::number(i++) + QLatin1String(" Rect Property"));
161     item->setValue(QRect(10, 10, 20, 20));
162     topItem->addSubProperty(item);
163     item->setAttribute(QLatin1String("constraint"), QRect(0, 0, 50, 50));
164
165     item = variantManager->addProperty(QVariant::RectF, QString::number(i++) + QLatin1String(" RectF Property"));
166     item->setValue(QRectF(1.2345, 1.2345, 1.2345, 1.2345));
167     topItem->addSubProperty(item);
168     item->setAttribute(QLatin1String("constraint"), QRectF(0, 0, 50, 50));
169     item->setAttribute(QLatin1String("decimals"), 3);
170
171     item = variantManager->addProperty(QtVariantPropertyManager::enumTypeId(),
172         QString::number(i++) + QLatin1String(" Enum Property"));
173     QStringList enumNames;
174     enumNames << "Enum0" << "Enum1" << "Enum2";
175     item->setAttribute(QLatin1String("enumNames"), enumNames);
176     item->setValue(1);
177     topItem->addSubProperty(item);
178
179     item = variantManager->addProperty(QtVariantPropertyManager::flagTypeId(),
180         QString::number(i++) + QLatin1String(" Flag Property"));
181     QStringList flagNames;
182     flagNames << "Flag0" << "Flag1" << "Flag2";
183     item->setAttribute(QLatin1String("flagNames"), flagNames);
184     item->setValue(5);
185     topItem->addSubProperty(item);
186
187     item = variantManager->addProperty(QVariant::SizePolicy, QString::number(i++) + QLatin1String(" SizePolicy Property"));
188     topItem->addSubProperty(item);
189
190     item = variantManager->addProperty(QVariant::Font, QString::number(i++) + QLatin1String(" Font Property"));
191     topItem->addSubProperty(item);
192
193     item = variantManager->addProperty(QVariant::Cursor, QString::number(i++) + QLatin1String(" Cursor Property"));
194     topItem->addSubProperty(item);
195
196     item = variantManager->addProperty(QVariant::Color, QString::number(i++) + QLatin1String(" Color Property"));
197     topItem->addSubProperty(item);
198
199     variantFactory = new QtVariantEditorFactory();
200
201     variantEditor = new QtTreePropertyBrowser();
202     variantEditor->setFactoryForManager(variantManager, variantFactory);
203     variantEditor->addProperty(topItem);
204     variantEditor->setPropertiesWithoutValueMarked(true);
205     variantEditor->setRootIsDecorated(false);
206
207     setWidget(variantEditor);
208
209     //variantEditor->show();
210
211 }
212
213 void NodePropertyWidget::createDisplayTreeProperty(){
214
215     variantManager = new QtVariantPropertyManager();
216
217     int i = 0;
218     topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),
219         QString::number(i++) + QLatin1String(" Display Property"));
220
221     item = variantManager->addProperty(QVariant::Bool, QString::number(i++) + QLatin1String(" Bool Property"));
222     item->setValue(true);
223     topItem->addSubProperty(item);
224
225     item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property"));
226     item->setValue(20);
227     item->setAttribute(QLatin1String("minimum"), 0);
228     item->setAttribute(QLatin1String("maximum"), 100);
229     item->setAttribute(QLatin1String("singleStep"), 10);
230     topItem->addSubProperty(item);
231
232     item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property (ReadOnly)"));
233     item->setValue(20);
234     item->setAttribute(QLatin1String("minimum"), 0);
235     item->setAttribute(QLatin1String("maximum"), 100);
236     item->setAttribute(QLatin1String("singleStep"), 10);
237     item->setAttribute(QLatin1String("readOnly"), true);
238     topItem->addSubProperty(item);
239
240     item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property"));
241     item->setValue(1.2345);
242     item->setAttribute(QLatin1String("singleStep"), 0.1);
243     item->setAttribute(QLatin1String("decimals"), 3);
244     topItem->addSubProperty(item);
245
246     item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property (ReadOnly)"));
247     item->setValue(1.23456);
248     item->setAttribute(QLatin1String("singleStep"), 0.1);
249     item->setAttribute(QLatin1String("decimals"), 5);
250     item->setAttribute(QLatin1String("readOnly"), true);
251     topItem->addSubProperty(item);
252
253     item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property"));
254     item->setValue("Value");
255     topItem->addSubProperty(item);
256
257     item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (Password)"));
258     item->setAttribute(QLatin1String("echoMode"), QLineEdit::Password);
259     item->setValue("Password");
260     topItem->addSubProperty(item);
261
262     // Readonly String Property
263     item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (ReadOnly)"));
264     item->setAttribute(QLatin1String("readOnly"), true);
265     item->setValue("readonly text");
266     topItem->addSubProperty(item);
267
268     item = variantManager->addProperty(QVariant::Date, QString::number(i++) + QLatin1String(" Date Property"));
269     item->setValue(QDate::currentDate().addDays(2));
270     topItem->addSubProperty(item);
271
272     item = variantManager->addProperty(QVariant::Time, QString::number(i++) + QLatin1String(" Time Property"));
273     item->setValue(QTime::currentTime());
274     topItem->addSubProperty(item);
275
276     item = variantManager->addProperty(QVariant::DateTime, QString::number(i++) + QLatin1String(" DateTime Property"));
277     item->setValue(QDateTime::currentDateTime());
278     topItem->addSubProperty(item);
279
280     item = variantManager->addProperty(QVariant::KeySequence, QString::number(i++) + QLatin1String(" KeySequence Property"));
281     item->setValue(QKeySequence(Qt::ControlModifier | Qt::Key_Q));
282     topItem->addSubProperty(item);
283
284     item = variantManager->addProperty(QVariant::Char, QString::number(i++) + QLatin1String(" Char Property"));
285     item->setValue(QChar(386));
286     topItem->addSubProperty(item);
287
288     item = variantManager->addProperty(QVariant::Locale, QString::number(i++) + QLatin1String(" Locale Property"));
289     item->setValue(QLocale(QLocale::Polish, QLocale::Poland));
290     topItem->addSubProperty(item);
291
292     item = variantManager->addProperty(QVariant::Point, QString::number(i++) + QLatin1String(" Point Property"));
293     item->setValue(QPoint(10, 10));
294     topItem->addSubProperty(item);
295
296     item = variantManager->addProperty(QVariant::PointF, QString::number(i++) + QLatin1String(" PointF Property"));
297     item->setValue(QPointF(1.2345, -1.23451));
298     item->setAttribute(QLatin1String("decimals"), 3);
299     topItem->addSubProperty(item);
300
301     item = variantManager->addProperty(QVariant::Size, QString::number(i++) + QLatin1String(" Size Property"));
302     item->setValue(QSize(20, 20));
303     item->setAttribute(QLatin1String("minimum"), QSize(10, 10));
304     item->setAttribute(QLatin1String("maximum"), QSize(30, 30));
305     topItem->addSubProperty(item);
306
307     item = variantManager->addProperty(QVariant::SizeF, QString::number(i++) + QLatin1String(" SizeF Property"));
308     item->setValue(QSizeF(1.2345, 1.2345));
309     item->setAttribute(QLatin1String("decimals"), 3);
310     item->setAttribute(QLatin1String("minimum"), QSizeF(0.12, 0.34));
311     item->setAttribute(QLatin1String("maximum"), QSizeF(20.56, 20.78));
312     topItem->addSubProperty(item);
313
314     item = variantManager->addProperty(QVariant::Rect, QString::number(i++) + QLatin1String(" Rect Property"));
315     item->setValue(QRect(10, 10, 20, 20));
316     topItem->addSubProperty(item);
317     item->setAttribute(QLatin1String("constraint"), QRect(0, 0, 50, 50));
318
319     item = variantManager->addProperty(QVariant::RectF, QString::number(i++) + QLatin1String(" RectF Property"));
320     item->setValue(QRectF(1.2345, 1.2345, 1.2345, 1.2345));
321     topItem->addSubProperty(item);
322     item->setAttribute(QLatin1String("constraint"), QRectF(0, 0, 50, 50));
323     item->setAttribute(QLatin1String("decimals"), 3);
324
325     item = variantManager->addProperty(QtVariantPropertyManager::enumTypeId(),
326         QString::number(i++) + QLatin1String(" Enum Property"));
327     QStringList enumNames;
328     enumNames << "Enum0" << "Enum1" << "Enum2";
329     item->setAttribute(QLatin1String("enumNames"), enumNames);
330     item->setValue(1);
331     topItem->addSubProperty(item);
332
333     item = variantManager->addProperty(QtVariantPropertyManager::flagTypeId(),
334         QString::number(i++) + QLatin1String(" Flag Property"));
335     QStringList flagNames;
336     flagNames << "Flag0" << "Flag1" << "Flag2";
337     item->setAttribute(QLatin1String("flagNames"), flagNames);
338     item->setValue(5);
339     topItem->addSubProperty(item);
340
341     item = variantManager->addProperty(QVariant::SizePolicy, QString::number(i++) + QLatin1String(" SizePolicy Property"));
342     topItem->addSubProperty(item);
343
344     item = variantManager->addProperty(QVariant::Font, QString::number(i++) + QLatin1String(" Font Property"));
345     topItem->addSubProperty(item);
346
347     item = variantManager->addProperty(QVariant::Cursor, QString::number(i++) + QLatin1String(" Cursor Property"));
348     topItem->addSubProperty(item);
349
350     item = variantManager->addProperty(QVariant::Color, QString::number(i++) + QLatin1String(" Color Property"));
351     topItem->addSubProperty(item);
352
353     variantFactory = new QtVariantEditorFactory();
354
355     variantEditor = new QtTreePropertyBrowser();
356     variantEditor->setFactoryForManager(variantManager, variantFactory);
357     variantEditor->addProperty(topItem);
358     variantEditor->setPropertiesWithoutValueMarked(true);
359     variantEditor->setRootIsDecorated(false);
360
361     setWidget(variantEditor);
362
363     //variantEditor->show();
364
365 }
366
367 void NodePropertyWidget::createArrowTreeProperty(){
368
369     variantManager = new QtVariantPropertyManager();
370
371     int i = 0;
372     topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),
373         QString::number(i++) + QLatin1String("Arrow Property"));
374
375     item = variantManager->addProperty(QVariant::Bool, QString::number(i++) + QLatin1String(" Bool Property"));
376     item->setValue(true);
377     topItem->addSubProperty(item);
378
379     item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property"));
380     item->setValue(20);
381     item->setAttribute(QLatin1String("minimum"), 0);
382     item->setAttribute(QLatin1String("maximum"), 100);
383     item->setAttribute(QLatin1String("singleStep"), 10);
384     topItem->addSubProperty(item);
385
386     item = variantManager->addProperty(QVariant::Int, QString::number(i++) + QLatin1String(" Int Property (ReadOnly)"));
387     item->setValue(20);
388     item->setAttribute(QLatin1String("minimum"), 0);
389     item->setAttribute(QLatin1String("maximum"), 100);
390     item->setAttribute(QLatin1String("singleStep"), 10);
391     item->setAttribute(QLatin1String("readOnly"), true);
392     topItem->addSubProperty(item);
393
394     item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property"));
395     item->setValue(1.2345);
396     item->setAttribute(QLatin1String("singleStep"), 0.1);
397     item->setAttribute(QLatin1String("decimals"), 3);
398     topItem->addSubProperty(item);
399
400     item = variantManager->addProperty(QVariant::Double, QString::number(i++) + QLatin1String(" Double Property (ReadOnly)"));
401     item->setValue(1.23456);
402     item->setAttribute(QLatin1String("singleStep"), 0.1);
403     item->setAttribute(QLatin1String("decimals"), 5);
404     item->setAttribute(QLatin1String("readOnly"), true);
405     topItem->addSubProperty(item);
406
407     item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property"));
408     item->setValue("Value");
409     topItem->addSubProperty(item);
410
411     item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (Password)"));
412     item->setAttribute(QLatin1String("echoMode"), QLineEdit::Password);
413     item->setValue("Password");
414     topItem->addSubProperty(item);
415
416     // Readonly String Property
417     item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (ReadOnly)"));
418     item->setAttribute(QLatin1String("readOnly"), true);
419     item->setValue("readonly text");
420     topItem->addSubProperty(item);
421
422     item = variantManager->addProperty(QVariant::Date, QString::number(i++) + QLatin1String(" Date Property"));
423     item->setValue(QDate::currentDate().addDays(2));
424     topItem->addSubProperty(item);
425
426     item = variantManager->addProperty(QVariant::Time, QString::number(i++) + QLatin1String(" Time Property"));
427     item->setValue(QTime::currentTime());
428     topItem->addSubProperty(item);
429
430     item = variantManager->addProperty(QVariant::DateTime, QString::number(i++) + QLatin1String(" DateTime Property"));
431     item->setValue(QDateTime::currentDateTime());
432     topItem->addSubProperty(item);
433
434     item = variantManager->addProperty(QVariant::KeySequence, QString::number(i++) + QLatin1String(" KeySequence Property"));
435     item->setValue(QKeySequence(Qt::ControlModifier | Qt::Key_Q));
436     topItem->addSubProperty(item);
437
438     item = variantManager->addProperty(QVariant::Char, QString::number(i++) + QLatin1String(" Char Property"));
439     item->setValue(QChar(386));
440     topItem->addSubProperty(item);
441
442     item = variantManager->addProperty(QVariant::Locale, QString::number(i++) + QLatin1String(" Locale Property"));
443     item->setValue(QLocale(QLocale::Polish, QLocale::Poland));
444     topItem->addSubProperty(item);
445
446     item = variantManager->addProperty(QVariant::Point, QString::number(i++) + QLatin1String(" Point Property"));
447     item->setValue(QPoint(10, 10));
448     topItem->addSubProperty(item);
449
450     item = variantManager->addProperty(QVariant::PointF, QString::number(i++) + QLatin1String(" PointF Property"));
451     item->setValue(QPointF(1.2345, -1.23451));
452     item->setAttribute(QLatin1String("decimals"), 3);
453     topItem->addSubProperty(item);
454
455     item = variantManager->addProperty(QVariant::Size, QString::number(i++) + QLatin1String(" Size Property"));
456     item->setValue(QSize(20, 20));
457     item->setAttribute(QLatin1String("minimum"), QSize(10, 10));
458     item->setAttribute(QLatin1String("maximum"), QSize(30, 30));
459     topItem->addSubProperty(item);
460
461     item = variantManager->addProperty(QVariant::SizeF, QString::number(i++) + QLatin1String(" SizeF Property"));
462     item->setValue(QSizeF(1.2345, 1.2345));
463     item->setAttribute(QLatin1String("decimals"), 3);
464     item->setAttribute(QLatin1String("minimum"), QSizeF(0.12, 0.34));
465     item->setAttribute(QLatin1String("maximum"), QSizeF(20.56, 20.78));
466     topItem->addSubProperty(item);
467
468     item = variantManager->addProperty(QVariant::Rect, QString::number(i++) + QLatin1String(" Rect Property"));
469     item->setValue(QRect(10, 10, 20, 20));
470     topItem->addSubProperty(item);
471     item->setAttribute(QLatin1String("constraint"), QRect(0, 0, 50, 50));
472
473     item = variantManager->addProperty(QVariant::RectF, QString::number(i++) + QLatin1String(" RectF Property"));
474     item->setValue(QRectF(1.2345, 1.2345, 1.2345, 1.2345));
475     topItem->addSubProperty(item);
476     item->setAttribute(QLatin1String("constraint"), QRectF(0, 0, 50, 50));
477     item->setAttribute(QLatin1String("decimals"), 3);
478
479     item = variantManager->addProperty(QtVariantPropertyManager::enumTypeId(),
480         QString::number(i++) + QLatin1String(" Enum Property"));
481     QStringList enumNames;
482     enumNames << "Enum0" << "Enum1" << "Enum2";
483     item->setAttribute(QLatin1String("enumNames"), enumNames);
484     item->setValue(1);
485     topItem->addSubProperty(item);
486
487     item = variantManager->addProperty(QtVariantPropertyManager::flagTypeId(),
488         QString::number(i++) + QLatin1String(" Flag Property"));
489     QStringList flagNames;
490     flagNames << "Flag0" << "Flag1" << "Flag2";
491     item->setAttribute(QLatin1String("flagNames"), flagNames);
492     item->setValue(5);
493     topItem->addSubProperty(item);
494
495     item = variantManager->addProperty(QVariant::SizePolicy, QString::number(i++) + QLatin1String(" SizePolicy Property"));
496     topItem->addSubProperty(item);
497
498     item = variantManager->addProperty(QVariant::Font, QString::number(i++) + QLatin1String(" Font Property"));
499     topItem->addSubProperty(item);
500
501     item = variantManager->addProperty(QVariant::Cursor, QString::number(i++) + QLatin1String(" Cursor Property"));
502     topItem->addSubProperty(item);
503
504     item = variantManager->addProperty(QVariant::Color, QString::number(i++) + QLatin1String(" Color Property"));
505     topItem->addSubProperty(item);
506
507     variantFactory = new QtVariantEditorFactory();
508
509     variantEditor = new QtTreePropertyBrowser();
510     variantEditor->setFactoryForManager(variantManager, variantFactory);
511     variantEditor->addProperty(topItem);
512     variantEditor->setPropertiesWithoutValueMarked(true);
513     variantEditor->setRootIsDecorated(false);
514
515     setWidget(variantEditor);
516     //variantEditor->show();
517
518 }

以下的参考中还会给出很多propertybrowser的很多例子

references:

https://github.com/commontk/QtPropertyBrowser

时间: 2024-10-11 06:55:40

Qt编程之实现属性窗口编辑器的相关文章

【Qt编程】无边框窗口的缩放与拖动

在现在,绝大多数软件都向着简洁,时尚发展.就拿有道的单词本和我做的单词本来说,绝大多数用户肯定喜欢我所做的单词本(就单单界面,关于颜色搭配和布局问题,大家就不要在意了). 有道的单词本: 我所做的单词本: 很明显,两者的主要区别就是周围的边框问题.你可以对比QQ以前的版本和这几年的版本,就会发现都倾向于下面这种窗口模式.下面我们就说说如何用Qt实现无边框窗口的缩放与拖动. 对于无边框窗口的拖动其实很简单,其基本思想是,在鼠标移动前后记录鼠标的坐标,然后将窗口移动这两个坐标之差的距离即可,具体实现

【Qt编程】无边框窗口的拖动

在上一篇文章中,我们讲述了如何进行无边框窗口的缩放与拖动,而在一些情况下,我们的窗口只需要进行拖动也不需要改变其大小,比如:QQ的登录窗口.本来在上一篇文章中已经讲述了如何进行窗口的拖动,但是却与窗口的缩放相关的程序放在一起,下面专门单独分离出来. 窗口的拖放只涉及到鼠标事件:按下操作.释放操作和移动操作,因此只需要重写这三个函数.由于程序比较简单,并且注释也比较详细,就不作过多介绍.新建一个基类为QWidget的Qt Gui应用程序,只需修改widget.h和widget.cpp文件如下: 1

qt编程

在QT编程中,对窗口进行菜单项和工具栏的添加,思路是先获取到整个窗口的菜单条,在菜单条中添加每一个的菜单项,然后再对菜单项添加一个个的动作Action,来实现功能.我使用的开发环境是Qt Creator5.5.0,默认生成的工程文件主文件中有ui->setupUi(this)这句代码,该代码意思大概就是将我们设置好的控件的属性和UI窗口进行链接,最终进行显示.但是如果在这句代码之前对菜单条进行菜单添加的话没有效果,要在这句代码之后进行添加,WindowTitle也是一样,而工具栏的添加则可以放在

Qt编程18:Qt调色板QPalette的使用

QPalette类有两个枚举类型, 枚举 1.ColorGroup CorGroup指的是3中不同的状态(什么时候设置颜色): 1>Active:获得焦点的状态. 2>Inactive:未获得焦点的状态. 3>Disable:不可用状态. 通常情况下,Active状态与Inactive状态下颜色显示是一致的,当然也可根据需要设置成不一样的颜色 2.ColorRole(设置什么颜色). ColorRole指的是颜色主题,即对窗体中不同部位颜色的分类,如QPalette::Window是指背

Qt编程获取鼠标移动事件

想做下面一个效果:想当鼠标移动到按钮上的时候,按钮就变大,图标换个大的,鼠标不在按钮上的时候,按钮就变小,图标也换成小的,感觉比较Cool 实现原理:为每个按钮设置监听属性 void MainHomeForm::init() { //为按钮注册事件 ui->SystemSetButton->installEventFilter(this); ui->ZoneSetButton->installEventFilter(this); } //系统监听器监听按钮对象 bool MainH

qt实现类似QQ伸缩窗口--鼠标事件应用

上一章节讲了qt鼠标事件实现,获取鼠标参数的方法.这一讲主要讲怎么应用上讲的鼠标事件实现一个小功能. qq好友对话框右侧 未展开时如图: 鼠标移动到 “隐藏侧边”处单击可以隐藏侧边,隐藏后效果如图: 实现的范例效果如下 1.一般情况 2.鼠标移动到中间分割线中间区域时,效果 3.单击红色按钮后,效果 4.鼠标移动到边界,效果 下面讲解实现过程:一般情况在QT中,这种伸缩窗体的实现有两种方法:一种是是直接用QSplitter分割器实现:另一种直接用布局管理器自己实现.本节用的是第二种方法实现的.布

Qt编程简介与基本知识

1. 什么是Qt? Qt是一个基于C++的跨平台应用程序和UI开发框架.它包含一个类库,和用于跨平台开发及国际化的工具. 由挪威Trolltech公司开发,后被Nokia收购,目前被Digia公司收购. 2. Qt的特性? 一次编程,到处编译 3. Qt的产生 4. Qt的发行版本 Qt商业版:提供给商业软件开发.它们提供传统商业软件发行版并且提供在协议有效期内的免费升级和技术支持服务. Qt开源版:仅仅为了开发自由和开放源码软件,提供了和商业版本同样的功能.GNU通用公共许可证下,它是免费的.

【Qt编程】系统托盘的显示

本文主要讨论Qt中的系统托盘的设置.系统托盘想必大家都不陌生,最常用的就是QQ.系统托盘以简单.小巧的形式能让人们较快的打开软件.废话不多说,下面开始具体介绍. 首先,新建一个Qt Gui项目,类型选择QMainWindow,类名也选择默认的mainwindow.这时,工程项目中就有3个文件(界面文件不用管):mainwindow.h mainwindow.cpp main.cpp.然后,分别修改3个文件如下: 1.mainwindow.h #ifndef MAINWINDOW_H #defin

在windows下的QT编程中的_TCHAR与QString之间的转换

由于在windows下的QT编程中,如果涉及到使用微软的API,那么不可避免使用_TCHAR这些类型,因此在网上查了一下,其中一个老外的论坛有人给出了这个转换,因此在这里做一下笔记 : )#ifdef UNICODE #define QStringToTCHAR(x)     (wchar_t*) x.utf16() #define PQStringToTCHAR(x)    (wchar_t*) x->utf16() #define TCHARToQString(x)     QString: