📄 qwidgetfactory.cpp
字号:
} } break; case Block_Toolbars: do { inputObject( objects, numObjects, strings, in, toplevel, toplevel, "QToolBar" ); } while ( !END_OF_BLOCK() ); break; case Block_Variables: // ### qWarning( "Block_Variables not supported" ); in.device()->at( nextBlock ); break; case Block_Widget: toplevel = (QWidget *) inputObject( objects, numObjects, strings, in, toplevel, parent, "QWidget" ); if ( toplevel != 0 ) toplevel->setName( name ); break; default: qWarning( "Version error" ); return 0; } in >> blockType; } delete[] objects; return toplevel;}/*! Installs a widget factory \a factory, which normally contains additional widgets that can then be created using a QWidgetFactory. See createWidget() for further details.*/void QWidgetFactory::addWidgetFactory( QWidgetFactory *factory ){ widgetFactories.append( factory );}/*! Creates a widget of the type \a className passing \a parent and \a name to its constructor. If \a className is a widget in the Qt library, it is directly created by this function. If the widget isn't in the Qt library, each of the installed widget plugins is asked, in turn, to create the widget. As soon as a plugin says it can create the widget it is asked to do so. It may occur that none of the plugins can create the widget, in which case each installed widget factory is asked to create the widget (see addWidgetFactory()). If the widget cannot be created by any of these means, 0 is returned. If you have a custom widget, and want it to be created using the widget factory, there are two approaches you can use: \list 1 \i Write a widget plugin. This allows you to use the widget in \e{Qt Designer} and in this QWidgetFactory. See the widget plugin documentation for further details. (See the "Creating Custom Widgets with Plugins" section of the \link designer-manual.book Qt Designer manual\endlink for an example. \i Subclass QWidgetFactory. Then reimplement this function to create and return an instance of your custom widget if \a className equals the name of your widget, otherwise return 0. Then at the beginning of your program where you want to use the widget factory to create widgets do a: \code QWidgetFactory::addWidgetFactory( new MyWidgetFactory ); \endcode where MyWidgetFactory is your QWidgetFactory subclass. \endlist*/QWidget *QWidgetFactory::createWidget( const QString &className, QWidget *parent, const char *name ) const{ // create widgets we know if ( className == "QPushButton" ) { return new QPushButton( parent, name ); } else if ( className == "QToolButton" ) { return new QToolButton( parent, name ); } else if ( className == "QCheckBox" ) { return new QCheckBox( parent, name ); } else if ( className == "QRadioButton" ) { return new QRadioButton( parent, name ); } else if ( className == "QGroupBox" ) { return new QGroupBox( parent, name ); } else if ( className == "QButtonGroup" ) { return new QButtonGroup( parent, name ); } else if ( className == "QIconView" ) {#if !defined(QT_NO_ICONVIEW) return new QIconView( parent, name );#endif } else if ( className == "QTable" ) {#if !defined(QT_NO_TABLE) return new QTable( parent, name );#endif } else if ( className == "QListBox" ) { return new QListBox( parent, name ); } else if ( className == "QListView" ) { return new QListView( parent, name ); } else if ( className == "QLineEdit" ) { return new QLineEdit( parent, name ); } else if ( className == "QSpinBox" ) { return new QSpinBox( parent, name ); } else if ( className == "QMultiLineEdit" ) { return new QMultiLineEdit( parent, name ); } else if ( className == "QLabel" || className == "TextLabel" || className == "PixmapLabel" ) { return new QLabel( parent, name ); } else if ( className == "QLayoutWidget" ) { return new QWidget( parent, name ); } else if ( className == "QTabWidget" ) { return new QTabWidget( parent, name ); } else if ( className == "QComboBox" ) { return new QComboBox( FALSE, parent, name ); } else if ( className == "QWidget" ) { if ( !qwf_stays_on_top ) return new QWidget( parent, name ); return new QWidget( parent, name, Qt::WStyle_StaysOnTop ); } else if ( className == "QDialog" ) { if ( !qwf_stays_on_top ) return new QDialog( parent, name ); return new QDialog( parent, name, FALSE, Qt::WStyle_StaysOnTop ); } else if ( className == "QWizard" ) { return new QWizard( parent, name ); } else if ( className == "QLCDNumber" ) { return new QLCDNumber( parent, name ); } else if ( className == "QProgressBar" ) { return new QProgressBar( parent, name ); } else if ( className == "QTextView" ) { return new QTextView( parent, name ); } else if ( className == "QTextBrowser" ) { return new QTextBrowser( parent, name ); } else if ( className == "QDial" ) { return new QDial( parent, name ); } else if ( className == "QSlider" ) { return new QSlider( parent, name ); } else if ( className == "QFrame" ) { return new QFrame( parent, name ); } else if ( className == "QSplitter" ) { return new QSplitter( parent, name ); } else if ( className == "Line" ) { QFrame *f = new QFrame( parent, name ); f->setFrameStyle( QFrame::HLine | QFrame::Sunken ); return f; } else if ( className == "QTextEdit" ) { return new QTextEdit( parent, name ); } else if ( className == "QDateEdit" ) { return new QDateEdit( parent, name ); } else if ( className == "QTimeEdit" ) { return new QTimeEdit( parent, name ); } else if ( className == "QDateTimeEdit" ) { return new QDateTimeEdit( parent, name ); } else if ( className == "QScrollBar" ) { return new QScrollBar( parent, name ); } else if ( className == "QPopupMenu" ) { return new QPopupMenu( parent, name ); } else if ( className == "QWidgetStack" ) { return new QWidgetStack( parent, name ); } else if ( className == "QVBox" ) { return new QVBox( parent, name ); } else if ( className == "QHBox" ) { return new QHBox( parent, name ); } else if ( className == "QGrid" ) { return new QGrid( 4, parent, name ); } else if ( className == "QMainWindow" ) { QMainWindow *mw = 0; if ( !qwf_stays_on_top ) mw = new QMainWindow( parent, name ); else mw = new QMainWindow( parent, name, Qt::WType_TopLevel | Qt::WStyle_StaysOnTop ); mw->setCentralWidget( new QWidget( mw, "qt_central_widget" ) ); mw->centralWidget()->show(); (void)mw->statusBar(); return mw; }#if !defined(QT_NO_SQL) else if ( className == "QDataTable" ) { return new QDataTable( parent, name ); } else if ( className == "QDataBrowser" ) { return new QDesignerDataBrowser2( parent, name ); } else if ( className == "QDataView" ) { return new QDesignerDataView2( parent, name ); }#endif setupPluginDir(); // try to create it using the loaded widget plugins if ( !widgetInterfaceManager ) widgetInterfaceManager = new QPluginManager<WidgetInterface>( IID_Widget, QApplication::libraryPaths(), *qwf_plugin_dir ); QInterfacePtr<WidgetInterface> iface = 0; widgetInterfaceManager->queryInterface( className, &iface ); if ( iface ) { QWidget *w = iface->create( className, parent, name ); if ( w ) { d->customWidgets.replace( className.latin1(), new bool(TRUE) ); return w; } } // hope we have a factory which can do it for ( QWidgetFactory* f = widgetFactories.first(); f; f = widgetFactories.next() ) { QWidget *w = f->createWidget( className, parent, name ); if ( w ) return w; } // no success return 0;}/*! Returns the names of the widgets, which this facory can create. */QStringList QWidgetFactory::widgets(){ setupWidgetListAndMap(); return *availableWidgetList;}/*! Returns whether this widget factory can create the widget \a widget */bool QWidgetFactory::supportsWidget( const QString &widget ){ setupWidgetListAndMap(); return ( availableWidgetMap->find( widget ) != availableWidgetMap->end() );}QWidget *QWidgetFactory::createWidgetInternal( const QDomElement &e, QWidget *parent, QLayout* layout, const QString &classNameArg ){ d->lastItem = 0; QDomElement n = e.firstChild().toElement(); QWidget *w = 0; // the widget that got created QObject *obj = 0; // gets the properties QString className = classNameArg; int row = e.attribute( "row" ).toInt(); int col = e.attribute( "column" ).toInt(); int rowspan = e.attribute( "rowspan" ).toInt(); int colspan = e.attribute( "colspan" ).toInt(); if ( rowspan < 1 ) rowspan = 1; if ( colspan < 1 ) colspan = 1; bool isQLayoutWidget = FALSE; if ( !className.isEmpty() ) { if ( !layout && className == "QLayoutWidget" ) { className = "QWidget"; isQLayoutWidget = TRUE; } if ( layout && className == "QLayoutWidget" ) { // hide layout widgets w = parent; } else { obj = QWidgetFactory::createWidget( className, parent, 0 ); if ( !obj ) return 0; w = (QWidget*)obj; if ( !toplevel ) toplevel = w; if ( w->inherits( "QMainWindow" ) ) w = ( (QMainWindow*)w )->centralWidget(); if ( layout ) { switch( layoutType( layout ) ) { case HBox: ( (QHBoxLayout*)layout )->addWidget( w ); break; case VBox: ( (QVBoxLayout*)layout )->addWidget( w ); break; case Grid: ( (QGridLayout*)layout )->addMultiCellWidget( w, row, row + rowspan - 1, col, col + colspan - 1 ); break; default: break; } } layout = 0; } }#ifdef QT_CONTAINER_CUSTOM_WIDGETS QString parentClassName = parent ? parent->className() : 0; bool isPlugin = parent ? !!d->customWidgets.find( parent->className() ) : FALSE; if ( isPlugin ) qWarning( "####### loading custom container widgets without page support not implemented!" ); // ### TODO loading for custom container widgets without pages#endif int idx = 0; while ( !n.isNull() ) { if ( n.tagName() == "spacer" ) { createSpacer( n, layout ); } else if ( n.tagName() == "widget" ) { QMap< QString, QString> *oldDbControls = dbControls; createWidgetInternal( n, w, layout, n.attribute( "class", "QWidget" ) ); dbControls = oldDbControls; } else if ( n.tagName() == "hbox" ) { QLayout *parentLayout = layout; if ( layout && layout->inherits( "QGridLayout" ) ) layout = createLayout( 0, 0, QWidgetFactory::HBox, isQLayoutWidget ); else layout = createLayout( w, layout, QWidgetFactory::HBox, isQLayoutWidget ); obj = layout; n = n.firstChild().toElement(); if ( parentLayout && parentLayout->inherits( "QGridLayout" ) ) ( (QGridLayout*)parentLayout )->addMultiCellLayout( layout, row, row + rowspan - 1, col, col + colspan - 1 ); continue; } else if ( n.tagName() == "grid" ) { QLayout *parentLayout = layout; if ( layout && layout->inherits( "QGridLayout" ) ) layout = createLayout( 0, 0, QWidgetFactory::Grid, isQLayoutWidget ); else layout = createLayout( w, layout, QWidgetFactory::Grid, isQLayoutWidget ); obj = layout; n = n.firstChild().toElement(); if ( parentLayout && parentLayout->inherits( "QGridLayout" ) ) ( (QGridLayout*)parentLayout )->addMultiCellLayout( layout, row, row + rowspan - 1, col, col + colspan - 1 ); continue; } else if ( n.tagName() == "vbox" ) { QLayout *parentLayout = layout; if ( layout && layout->inherits( "QGridLayout" ) ) layout = createLayout( 0, 0, QWidgetFactory::VBox, isQLayoutWidget ); else layout = createLayout( w, layout, QWidgetFactory::VBox, isQLayoutWidget ); obj = layout; n = n.firstChild().toElement(); if ( parentLayout && parentLayout->inherits( "QGridLayout" ) ) ( (QGridLayout*)parentLayout )->addMultiCellLayout( layout, row, row + rowspan - 1, col, col + colspan - 1 ); continue; } else if ( n.tagName() == "property" && obj ) { setProperty( obj, n.attribute( "name" ), n.firstChild().toElement() ); } else if ( n.tagName() == "attribute" && w ) { QString attrib = n.attribute( "name" ); QVariant v = DomTool::elementToVariant( n.firstChild().toElement(), QVariant() ); if ( parent ) { if ( parent->inherits( "QTabWidget" ) ) { if ( attrib == "title" ) ( (QTabWidget*)parent )->insertTab( w, v.toString() ); } else if ( parent->inherits( "QWidgetStack" ) ) { if ( attrib == "id" ) ( (QWidgetStack*)parent )->addWidget( w, v.toInt() ); } else if ( parent->inherits( "QWizard" ) ) { if ( attrib == "title" ) ( (QWizard*)parent )->addPage( w, v.toString() );#ifdef QT_CONTAINER_CUSTOM_WIDGETS } else if ( isPlugin ) { if ( attrib == "label" ) { WidgetInterface *iface = 0; widgetInterfaceManager->queryInterface( parentClassName, &iface ); if ( iface ) { QWidgetContainerInterfacePrivate *iface2 = 0; iface->queryInterface( IID_QWidgetContainer, (QUnknownInterface**)&iface2 ); if ( iface2 ) { iface2->insertPage( parentClassName, (QWidget*)parent, v.toString(), -1, w ); iface2->release(); } iface->release(); } }#endif } } } else if ( n.tagName() == "item" ) { createItem( n, w ); } else if ( n.tagName() == "column" || n.tagName() == "row" ) { createColumn( n, w ); } n = n.nextSibling().toElement(); idx++; } return w;}QLayout *QWidgetFactory::createLayout( QWidget *widget, QLayout* layout, LayoutType type, bool isQLayoutWidget ){ int spacing = defSpacing; int margin = defMargin; if ( layout || !widget || isQLayoutWidget ) margin = 0; if ( !layout && widget && widget->inherits( "QTabWidget" ) ) widget = ((QTabWidget*)widget)->currentPage(); if ( !layout && widget && widget->inherits( "QWizard" ) ) widget = ((QWizard*)widget)->currentPage(); if ( !layout && widget && widget->inherits( "QWidgetStack" ) ) widget = ((QWidgetStack*)widget)->visibleWidget(); QLayout *l = 0; int align = 0; if ( !layout && widget && widget->inherits( "QGroupBox" ) ) { QGroupBox *gb = (QGroupBox*)widget; gb->setColumnLayout( 0, Qt::Vertical ); layout = gb->layout(); layout->setMargin( 0 ); layout->setSpacing( 0 ); align = Qt::AlignTop; } if ( layout ) { switch ( type ) { case HBox: l = new QHBoxLayout( layout ); break; case VBox: l = new QVBoxLayout( layout ); break; case Grid: l = new QGridLayout( layout ); break; default: return 0; } } else { switch ( type ) { case HBox: l = new QHBoxLayout( widget ); break; case VBox: l = new QVBoxLayout( widget ); break; case Grid: l = new QGridLayout( widget );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -