• 6854阅读
  • 4回复

QT3.*文档中的一个问题 [复制链接]

上一主题 下一主题
离线tomcruiz
 

只看楼主 倒序阅读 楼主  发表于: 2006-04-18
在QT3.*文档中,我读到Subclassing and Dynamic Dialogs这一节的时候,看代码没有看懂。
Implementing Slots for Dynamic Dialogs
There is one outstanding issue that we haven't addressed: the dynamic dialog does not have the behaviour of the original credit dialog because we have not implemented the setAmount() slot. We can implement slots for dynamic dialogs by creating a QObject subclass. We then create an instance of this subclass and pass a pointer to it to the QWidgetFactory::create() function which will connect the dynamic dialog's signals to the slots implemented in our subclass.

We need to create a QObject subclass and change our creditDialog() to create an instance of our subclass that can be passed to the QWidgetFactory::create() function. Here is the modified creditDialog() function from the qt/tools/designer/examples/receiver2/mainform.ui.h file that contains the code for mainform.ui's slots:

  void MainForm::creditDialog()
  {
    Receiver *receiver = new Receiver;
    QDialog *creditForm = (QDialog *)
    QWidgetFactory::create( "../credit/creditformbase.ui", receiver );
    receiver->setParent( creditForm );

    // Set up the dynamic dialog here

    if ( creditForm->exec() ) {
        // The user accepted, act accordingly
        QSpinBox *amount = (QSpinBox *) creditForm->child( "amountSpinBox", "QSpinBox" );
        if ( amount )
          ratingTextLabel->setText( amount->text() );
    }

    delete receiver;
    delete creditForm;
  }
这个receiver类在这里起到的作用是什么啊?
上面写到的是:connect the dynamic dialog's signals to the slots implemented in our subclass

create函数返回了一个dialog指针给creditForm的意思,就是把这个create的窗口的操作权交给creditForm咯?
那receiver->setParent( creditForm );这一句又做何用途呢?
[ 此贴被XChinux在2006-04-18 16:17重新编辑 ]
离线tomcruiz

只看该作者 1楼 发表于: 2006-04-18
下面还有一句话,似乎解释了这个问题:
We create a new instance of our 'Receiver' subclass. (We'll write the code for this class shortly.) We then create the QDialog using QWidgetFactory::create(). This call differs from our previous example because we pass in the subclass object so that the create() function can set up the signals/slots connections automatically for us. Since our slot must access the widgets in the dynamic form we pass a pointer to the form to the receiver object through our setParent() function. The remainder of the function is the same as before except that we delete our receiver object.

这段话中的 the create() function can set up the signals/slots connections automatically for us.这个不太明白阿。是怎么动态连接信号槽的呢?

附上Receiver 类的实现:
#include <qobject.h>
#include <qdialog.h>

class Receiver : public QObject
{
  Q_OBJECT
public:
  void setParent( QDialog *parent );
public slots:
  void setAmount();
private:
  QDialog *p;
};


  #include <qradiobutton.h>
  #include <qspinbox.h>
  #include "receiver.h"
  void Receiver::setParent( QDialog *parent )
  {
    p = parent;
    setAmount();
  }
void Receiver::setAmount()
  {
    QSpinBox *amount =
        (QSpinBox *) p->child( "amountSpinBox", "QSpinBox" );

    QRadioButton *radio =
        (QRadioButton *) p->child( "stdRadioButton", "QRadioButton" );
    if ( radio && radio->isChecked() ) {
        if ( amount )
          amount->setValue( amount->maxValue() / 2 );
        return;
    }

    radio =
        (QRadioButton *) p->child( "noneRadioButton", "QRadioButton" );
    if ( radio && radio->isChecked() )
        if ( amount )
          amount->setValue( amount->minValue() );
  }
离线tomcruiz

只看该作者 2楼 发表于: 2006-04-18
有没有用过QT3.*的大哥阿?帮小弟指点一下迷津
离线billdates
只看该作者 3楼 发表于: 2006-04-19
notice the following comment in the doc: "Since the .ui file is not compiled it cannot include any C++ code". This means it cannot have things like Q_OBJECT defined so it cannot handle signal/slots. Therefore you need a helper class. Receiver is such a class. It can define Q_OBJECT and thus be able to handle signal/slots.

The line

QWidgetFactory::create( "../credit/creditformbase.ui", receiver );

calls

QWidget * QWidgetFactory::create ( const QString & uiFile, QObject * connector = 0, QWidget * parent = 0, const char * name = 0 ) [static]

Loads the Qt Designer user interface description file uiFile and returns the top-level widget in that description. parent and name are passed to the constructor of the top-level widget.

This function also performs signal and slot connections, tab ordering, etc., as described in the .ui file. In Qt Designer it is possible to add custom slots to a form and connect to them. If you want these connections to be made, you must create a class derived from QObject, which implements all these slots. Then pass an instance of the object as connector to this function. If you do this, the connections to the custom slots will be done using the connector as slot.

If something fails, 0 is returned.

The ownership of the returned widget is passed to the caller.

Look at the source code of the function and you will understand more.
Hope this helps.
离线tomcruiz

只看该作者 4楼 发表于: 2006-04-19
恩,我看明白了~~~~,这个函数的含义我还没有搞懂。。看来文档还得看的再仔细些,感谢阿!!!
快速回复
限100 字节
 
上一个 下一个