QT Note

1、

        在用到QtNetwork/QTcpSocket时,需要再生成的.pro文件里添加:

        QT += network

2、

        在编译使用QtNetwork/QTcpSocket的代码时,在cmd下给.pro添加“QT += network”后用“mingw32-make”编译makefile,仍会报undefined reference错误;但是用make就可以编译通过。

        在Eclipse下用的也是mingw32-make命令,但是就没有这个问题。

3、EventFiler

http://developer.nokia.com/Community/Wiki/Event_Filters_in_Qt

        EventFilters allow an object A to receive the events of another object B. For each event of B that A receives, A can then either forward it to B or remove it from B's event stream.Before using the filter events the Event Filter must be installed. To do this we call install EventFilter() in the constructor of the object A that is to monitor the events of object B.

         b->installEventFilter(this);

        Here b is a pointer o B. Now B gives up all its events to A and leaves A with the decision whether it should filter out the event or let it through to B.

        For this purpose an event Filter() method is used, which has the following signature:

                    boolQObject::eventFilter(QObject*watched,QEvent*e);

        This must be reimplemented by A. The watched parameter allows events from several monitored objects to be distinguished from one another, and e is the event to be processed.

        The return value tells the event system how it should proceed with the event. If false is returned, it is forwarded to the monitored object, whereas true causes it to be filtered out. This means that the event does not arrive at the object for which it was originally intended.

4、指针越界

       今天调试代码时,一直报如下错误:


       原因是指针越界,错误代码如下:

QList<QString> IP;
QList<QString>::iterator iteratorIP;
void connectToHost()
{
	for(iteratorIP = IP.begin(); iteratorIP != IP.end(); iteratorIP++)
		t->connectToHost(*iteratorIP);
}

      越界原因是:在经过该for循环后,iteratorIP已经指向了IP.end(),其他函数再调用iteratorIP的话,就指向了未初始化内存。

         注意类似错误,报错指向的语句总是含有指针,就应该想到可能是类似错误。

5、signal

       Signals can be connected to other signals.

6、构造函数的内容

       QT类的构造函数通常做如下几件事:1)定义对象;2)初始化界面上如button, label等的状态;3)建立信号和槽的连接。

       其中定义对象分两种:1)在类的声明中先定义指向该类的指针,然后在构造函数中new一个该类的对象赋给该指针;2)直接在构造函数里new一个新的对象;

       分别在何种情况下使用这两种不同的方法?如果只在构造函数里用到new的对象,则用方法“2”;如果其他的成员函数也有用到该对象,则用方法“1”。

7、QDialog的使用

         Note that QDialog (and any other widget that has type Qt::Dialog) uses the parent widget slightly differently from other classes in Qt. A dialog is always a top-level widget,but if it has a parent, its default location is centered on top of the parent'stop-level widget (if it is not top-level itself).

         int QDialog::exec () [slot]

         Shows the dialog as a modal dialog,  blocking until the user closes it. 

7、paintEvent

         void QWidget::paintEvent ( QPaintEvent * event ) [virtualprotected]

         This event handler can be reimplemented in a subclass to receive paint events passed in event.

         A paint event is a request to repaint all or part of a widget. It can happen for one of the following reasons:

         1) repaint() or update() was invoked,

         2) the widget was obscured and has now been uncovered, or

         3) many other reasons.

8、QSize

         The QSize class defines the size of a two-dimensional object using integer point precision.

9、QTableWidget

         Row,column都是从0开始计数的。

10、connect

         在TelnetClient的构造函数中有如下connect:

TelnetClient::TelnetClient()

{

         Connect(disconnectButton,SIGNAL(clicked()), this, SLOT(t->close())); //wrong

         Connect(disconnectButton,SIGNAL(clicked()), t, SLOT(close()));

}

         其中,t是类TelnetClient里定义的指向QtTelnet的指针,目的是将disconnectButton与QtTelnet的close函数connect起来,注意第一种方法是错误的,因为t->close()不是this对象的slot函数,只能使用第二种。

        connect中信号和槽的连接类型包括:AutoConnectionDirectConnectionQueuedConnectionConnect类型决定了slot的运行顺序,是立即响应 signal执行,还是返回到receiver’s thread之后再排队执行。它并不影响该槽函数在哪个线程里执行。

11、QWidget

         The QWidget class is the base class of all user interface objects.

         所有的UI类都是QWidget的子类,因此可以看到:QWidget用于所有UI类的构造函数。

12、QObject

         The QObject class is the base class of all QT objects.

         所有的类都是QObject的子类,因此可以看到:QObject用于所有非UI类的构造函数,如QtcpSocket,当然QObject也可以用在UI类的构造函数中。

13、moc

        The Meta-ObjectCompiler, moc, is the program that handles Qt's C++ extensions.

        The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.

14、unresolved错误

         可以先将VS工程中GeneratedFiles的所有文件先remove,再重新从电脑中把GeneratedFiles目录下的文件添加到VS工程中。

15、注意代码的执行顺序

         如下列代码:

void MainWindow::connectPPC()
{
	for (iteratorIP = IPList.begin(); ++iteratorIP != IPList.end() && !IPList.isEmpty(); )
	{
		telnetPPC->connectPPC(*iteratorIP);
	}
	iteratorIP--;
}

void MainWindow::connectBusy()
{
	telnetPPC->disconncetPPC();
}

connect(telnetPPC, SIGNAL(busy()), this, SLOT(connectBusy()));

     在for循环里会调用telnetPPC的connectPPC接口,该接口内部会调用socket来实现连接PPC,每个*iteratorIP对应一个PPC。假设这个动作会触发telnetPPC的emit busy()信号;并且有如上所示的signal-slot关系。

     问题:代码的执行顺序是怎样的?

     a)在for循环内每执行一次connectPPC,立即触发一次connectBusy();

     b)在for循环内尝试连接所有的PPC后,再连续触发connectBusy() n次(假设共有n个PPC)

     c)在for循环内尝试连接所有PPC后,再触发一次connectBusy()一次;

     实际的执行顺序是c。

     这也就解释了,如果在for循环执行完之后没有执行”iteratorIP- -“的话,报读内存错误的原因。因为若没有”iteratorIP- -“,iteratorIP将指向IPList.end(),IPList.end()是没有指向任何成员的。

     iteratorQList::end ()

     Returns anSTL-style iterator pointing to the imaginary item after the last item in thelist.

     现改为:不用for循环,而用QTimer来触发connectPPC,执行顺序由QTimer的Interval决定的。Interval大的话,就会connect一次,emit一次;interval小的话,就会导致connect多次后,才emit一次。

16、QList::indexOf()

        intQList::indexOf ( const T & value, int from = 0 ) const

        This function requires the value type to have an implementation of operator==().

         假如在定义QList是,链表元素的类型是struct或者class,要使用indexOf()的话,该类必须重载该struct或class类型的”==”运算符。

如下所示代码,必须重载”= =”,才能使用链表。

Typedef struct
{
         QStringip;
         QStringstatus;
}IPStautsStruct;
QList<IPStatusStruct> IPStatusList;
Bool operator== ( const IPStatusStruct &lIP,const IPStatusStruct &rIP);

17、assignable data types

      QMap'skey and value data types must be assignable data types.This covers most data types you are likely to encounter, but the compiler won'tlet you, for example, store a QWidget as a value; instead, store a QWidget *.

      The values stored in the various containers can be of anyassignable datatype. To qualify, a type must provide a default constructor,a copy constructor, and an assignment operator. This covers most data types you are likely to want to store in a container, including basictypes such as int and double, pointer types, and Qt data types such as QString,QDate, and QTime,but it doesn't cover QObject or any QObject subclass (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a QList<QWidget>, the compiler will complain that QWidget's copy constructor and assignment operators are disabled.If you want to store these kinds of objects in a container, store them as pointers,for example as QList<QWidget *>.


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值