C++/Qt/QUiLoader

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Qt UI loader

  
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class MyWidget : public QWidget
{
public:
    MyWidget(QWidget *parent = 0);
};
#endif



#include <QtGui>
#include <QtUiTools>
#include "mywidget.h"

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    QUiLoader loader;
    QFile file(":/forms/myform.ui");
    file.open(QFile::ReadOnly);
    QWidget *myWidget = loader.load(&file, this);
    file.close();
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(myWidget);
    setLayout(layout);
}



<ui version="4.0" >
 <author></author>
 <comment></comment>
 <exportmacro></exportmacro>
 <class>Form</class>
 <widget class="QWidget" name="Form" >
  <property name="geometry" >
   <rect>
    <x>0</x>
    <y>0</y>
    <width>258</width>
    <height>224</height>
   </rect>
  </property>
  <property name="windowTitle" >
   <string>Export Document</string>
  </property>
  <layout class="QVBoxLayout" >
   <property name="margin" >
    <number>8</number>
   </property>
   <property name="spacing" >
    <number>6</number>
   </property>
   <item>
    <widget class="QGroupBox" name="groupBox" >
     <property name="title" >
      <string>Export Options</string>
     </property>
     <layout class="QGridLayout" >
      <property name="margin" >
       <number>8</number>
      </property>
      <property name="spacing" >
       <number>6</number>
      </property>
      <item row="1" column="0" >
       <widget class="QRadioButton" name="radioButton_2" >
        <property name="text" >
         <string>&amp;DocBook</string>
        </property>
       </widget>
      </item>
      <item row="0" column="0" >
       <widget class="QRadioButton" name="radioButton" >
        <property name="text" >
         <string>&amp;LaTeX</string>
        </property>
        <property name="checked" >
         <bool>true</bool>
        </property>
       </widget>
      </item>
      <item row="1" column="1" >
       <widget class="QCheckBox" name="checkBox_2" >
        <property name="text" >
         <string>Include p&amp;ictures</string>
        </property>
        <property name="checked" >
         <bool>true</bool>
        </property>
       </widget>
      </item>
      <item row="5" column="0" >
       <spacer>
        <property name="orientation" >
         <enum>Qt::Vertical</enum>
        </property>
        <property name="sizeHint" >
         <size>
          <width>20</width>
          <height>40</height>
         </size>
        </property>
       </spacer>
      </item>
      <item row="0" column="1" >
       <widget class="QCheckBox" name="checkBox" >
        <property name="text" >
         <string>&amp;Compress</string>
        </property>
       </widget>
      </item>
      <item row="2" column="0" >
       <widget class="QRadioButton" name="radioButton_2_2" >
        <property name="text" >
         <string>&amp;HTML</string>
        </property>
       </widget>
      </item>
      <item row="3" column="0" >
       <widget class="QRadioButton" name="radioButton_3" >
        <property name="text" >
         <string>&amp;PostScript</string>
        </property>
       </widget>
      </item>
      <item row="4" column="0" >
       <widget class="QRadioButton" name="radioButton_4" >
        <property name="text" >
         <string>PD&amp;F</string>
        </property>
       </widget>
      </item>
      <item row="2" column="1" >
       <widget class="QCheckBox" name="checkBox_3" >
        <property name="text" >
         <string>Include &amp;metadata</string>
        </property>
       </widget>
      </item>
      <item row="3" column="1" >
       <widget class="QCheckBox" name="checkBox_4" >
        <property name="text" >
         <string>Create inde&amp;x</string>
        </property>
        <property name="checked" >
         <bool>true</bool>
        </property>
       </widget>
      </item>
     </layout>
    </widget>
   </item>
  </layout>
 </widget>
 <pixmapfunction></pixmapfunction>
 <resources/>
 <connections/>
</ui>




#include <QtGui>
#include <QtUiTools>
#include "mywidget.h"

QWidget *loadCustomWidget(QWidget *parent)
{
    QUiLoader loader;
    QWidget *myWidget;
    QStringList availableWidgets = loader.availableWidgets();
    if (availableWidgets.contains("AnalogClock"))
        myWidget = loader.createWidget("AnalogClock", parent);
    return myWidget;
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyWidget widget;
    widget.show();
    QWidget *customWidget = loadCustomWidget(0);
    customWidget->show();
    return app.exec();
}


Qt UI test

  

Foundations of Qt Development\Chapter16\widgetdata\main.cpp
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <QtTest>
#include "spinboxtest.h"
QTEST_MAIN(SpinBoxTest)

Foundations of Qt Development\Chapter16\widgetdata\spinboxtest.cpp
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <QtTest>
#include <QSpinBox>
#include "spinboxtest.h"
Q_DECLARE_METATYPE( Qt::Key )
void SpinBoxTest::testKeys()
{
  QSpinBox spinBox;
  spinBox.setRange( 1, 10 );
  
  QFETCH( Qt::Key, key );
  QFETCH( int, startValue );
  
  spinBox.setValue( startValue );
  QTest::keyClick( &spinBox, key );
  QTEST( spinBox.value(), "endValue" );
}
void SpinBoxTest::testKeys_data()
{
  QTest::addColumn<Qt::Key>( "key" );
  QTest::addColumn<int>( "startValue" );
  QTest::addColumn<int>( "endValue" );
    
  QTest::newRow( "Up" ) << Qt::Key_Up << 5 << 6;
  QTest::newRow( "Down" ) << Qt::Key_Down << 5 << 4;
  QTest::newRow( "Up, limit" ) << Qt::Key_Up << 10 << 10;
  QTest::newRow( "Down, limit" ) << Qt::Key_Down << 1 << 1;
}
void SpinBoxTest::testClicks()
{
  QSpinBox spinBox;  
  spinBox.setRange( 1, 10 );
  
  QSize size = spinBox.size();
  QPoint upButton = QPoint( size.width()-2, 2 );
  QPoint downButton = QPoint( size.width()-2, size.height()-2 );
  QFETCH( QString, direction );
  QFETCH( int, startValue );
  
  spinBox.setValue( startValue );
  
  if( direction.toLower() == "up" )
    QTest::mouseClick( &spinBox, Qt::LeftButton, 0, upButton );
  else if (direction.toLower() == "down" )
    QTest::mouseClick( &spinBox, Qt::LeftButton, 0, downButton );
  else
    QWARN( "Unknown direction - no clicks issued." );
  
  QTEST( spinBox.value(), "endValue" );
}
void SpinBoxTest::testClicks_data()
{
  QTest::addColumn<QString>( "direction" );
  QTest::addColumn<int>( "startValue" );
  QTest::addColumn<int>( "endValue" );
    
  QTest::newRow( "Up" ) << "Up" << 5 << 6;
  QTest::newRow( "Down" ) << "Down" << 5 << 4;
  QTest::newRow( "Up, limit" ) << "Up" << 10 << 10;
  QTest::newRow( "Down, limit" ) << "Down" << 1 << 1;
}
void SpinBoxTest::testSetting()
{
  QSpinBox spinBox;
  spinBox.setRange( 1, 10 );
  
  QFETCH( int, value );
  spinBox.setValue( value );
  QTEST( spinBox.value(), "endValue" );
}
void SpinBoxTest::testSetting_data()
{
  QTest::addColumn<int>( "value" );
  QTest::addColumn<int>( "endValue" );
    
  QTest::newRow( "Valid" ) << 5 << 5;
  QTest::newRow( "Over" ) << 11 << 10;
  QTest::newRow( "Under" ) << 0 << 1;
}

Foundations of Qt Development\Chapter16\widgetdata\spinboxtest.h
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#ifndef SPINBOXTEST_H
#define SPINBOXTEST_H
#include <QObject>
class SpinBoxTest : public QObject
{
  Q_OBJECT
private slots:
  void testKeys();
  void testKeys_data();
  
  void testClicks();
  void testClicks_data();
  void testSetting();
  void testSetting_data();
};
#endif // SPINBOXTEST_H