1. 程式人生 > >Qt文件----Signals and Slots

Qt文件----Signals and Slots

譯者注:Qt是一套跨平臺的C++ GUI應用程式框架,最近非常流行。它的訊號與插槽(Signals and Slots)機制與Windows的訊息機制有很大差別,特此翻譯其手冊中這一篇,為學習亦為好奇,若錯誤百出還請指正為謝。

Signals and Slots

Signals and slots are used for communication between objects. The signal/slot mechanism is a central feature of Qt and probably the part that differs most from other toolkits.
訊號與插槽用於物件間通訊。訊號/插槽機制是Qt的重要特徵,這也許就是它與其它工具包差別最大之處。

In GUI programming we often want a change in one widget to be notified to another widget. More generally, we want objects of any kind to be able to communicate with one another. For example if we were parsing an XML file we might want to notify a list view that we're using to represent the XML file's structure whenever we encounter a new tag.
在圖形使用者介面程式設計中,我們常常希望一個視窗接收到其他視窗的通知之時做出改變。更普遍地,我們希望任意型別的物件能夠與其它物件通訊。例如,在我們解析XML檔案時,我們也許希望在遇到一個新的標記時,能夠給一個我們用來顯示XML檔案的結構的列表發出通知。

Older toolkits achieve this kind of communication using callbacks. A callback is a pointer to a function, so if you want a processing function to notify you about some event you pass a pointer to another function (the callback) to the processing function. The processing function then calls the callback when appropriate. Callbacks have two fundamental flaws. Firstly they are not type safe. We can never be certain that the processing function will call the callback with the correct arguments. Secondly the callback is strongly coupled to the processing function since the processing function must know which callback to call.
老的工具包使用回撥來達到這樣的目的。回撥是一個指向函式的指標,所以如果你希望一個處理函式通知你某些事件發生了,你可以傳遞一個指向其他函式的指標(回撥)給處理函式。處理函式會在適當的時候呼叫回撥函式。回撥有兩個基本缺點。第一,它不是型別安全的。我們不能保證處理函式會以正確的引數來回調。第二,回撥與處理函式強耦合,因為處理函式必須知道哪個回撥被呼叫。

Old-fashioned callbacks impair component programming
In Qt we have an alternative to the callback technique. We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many pre-defined signals, but we can always subclass to add our own. A slot is a function that is called in reponse to a particular signal. Qt's widgets have many pre-defined slots, but it is common practice to add your own slots so that you can handle the signals that you are interested in.
在Qt中,我們有了除回撥技術的另外的選擇。我們使用訊號與插槽。當特殊事件發生的時候,訊號就被髮出了。Qt的視窗有許多預定義訊號,但我們也可以新增我們自己的。一個插槽就是一個函式,被稱作特定訊號的響應。

Signals and Slots facilitate true type-safe component programming
The signals and slots mechanism is type safe: the signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) Since the signatures are compatible, the compiler can help us detect type mismatches. Signals and slots are loosely coupled: a class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type. They are completely typesafe: no more callback core dumps!
訊號與插槽機制是型別安全的:訊號的簽名必須和接收插槽的簽名匹配。(事實上插槽的簽名或許比它收到的訊號的簽名短,因為它可以忽略掉額外的引數。)因為簽名是相容的,編譯器可以幫助我們檢測出型別不匹配。訊號與插槽是鬆耦合的:一個發出訊號的類不知道也不關心哪一個插槽接收到這個訊號。Qt的訊號和插槽機制保證瞭如果你連線一個訊號到一個插槽上,插槽會在正確的時間,被以訊號的引數呼叫。訊號與插槽可以攜帶任意個、任意型別的引數。他們完全型別安全,沒有回撥的核心傳儲(core dumps)!

All classes that inherit from QObject or one of its subclasses (e.g. QWidget) can contain signals and slots. Signals are emitted by objects when they change their state in a way that may be interesting to the outside world. This is all the object does to communicate. It does not know or care whether anything is receiving the signals it emits. This is true information encapsulation, and ensures that the object can be used as a software component.
所有從Qobject或者它的一個子類(比如:QWidget)繼承的類都包含訊號與插槽。在物件以某種方式改變了它可能讓外界感興趣的狀態時,訊號就會被物件激發。這就是所有物件通訊所做的。它並不知道也不感興趣接收什麼。這是真正的資訊封裝,保證了物件能被當作軟體元件來使用。

Slots can be used for receiving signals, but they are normal member functions. A slot does not know if it has any signals connected to it. Again, the object does not know about the communication mechanism and can be used as a true software component.
插槽能被用來接收訊號,但它們是普通成員函式。插槽不知道是否有訊號連線到它。物件也不知道通訊機制,可以作為真正的軟體元件來使用。

You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you desire. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)
你可以連線任意訊號到你希望的單一插槽,並且一個訊號也能夠被連線到你所期望的多個插槽。甚至可能直接連線一個訊號到其他訊號。(這意味當前一個訊號被激發時,會立即激發另一個訊號)。

Together, signals and slots make up a powerful component programming mechanism.
總之,訊號與插槽建立了強大的元件程式設計機制。

A Small Example
A minimal C++ class declaration might read:
一個最小的C++類可以宣告如下:
    class Foo
    {
    public:
        Foo();
        int value() const { return val; }
        void setValue( int );
    private:
        int val;
    };
A small Qt class might read:
一個小的Qt類可以像這樣:
    class Foo : public QObject
    {
        Q_OBJECT
    public:
        Foo();
        int value() const { return val; }
    public slots:
        void setValue( int );
    signals:
        void valueChanged( int );
    private:
        int val;
    };
This class has the same internal state, and public methods to access the state, but in addition it has support for component programming using signals and slots: this class can tell the outside world that its state has changed by emitting a signal, valueChanged(), and it has a slot which other objects may send signals to.
這個類擁有相同的內部狀態和訪問狀態的公有方法,但另外它擁有利用訊號與插槽進行元件程式設計的支援:這個類能夠通過激發一個訊號valueChanged()來告訴外界它的狀態已改變,並且它也有一個可以供其他物件傳送訊號的插槽。

All classes that contain signals and/or slots must mention Q_OBJECT in their declaration.
所有包含訊號和/或插槽的類必須在它們的聲明裡新增Q_OBJECT。

Slots are implemented by the application programmer. Here is a possible implementation of Foo::setValue():
插槽由應用程式設計師實現,這是Foo::setValue()的一種可能的實現:
    void Foo::setValue( int v )
    {
        if ( v != val ) {
            val = v;
            emit valueChanged(v);
        }
    }

The line emit valueChanged(v) emits the signal valueChanged from the object. As you can see, you emit a signal by using emit signal(arguments).
emit valueChanged(v)
這行從物件裡激發了訊號valueChanged。正如你所能看到的,你使用emit 訊號(引數)激發一個訊號。

Here is one way to connect two of these objects together:
這裡有一種將兩個這種物件連線到一起的方法。
    Foo a, b;
    connect(&a, SIGNAL(valueChanged(int)), &b, SLOT(setValue(int)));
    b.setValue( 11 ); // a == undefined  b == 11
    a.setValue( 79 ); // a == 79         b == 79
    b.value();       

Calling a.setValue(79) will make a emit a valueChanged() signal, which b will receive in its setValue() slot, i.e. b.setValue(79) is called. b will then, in turn, emit the same valueChanged() signal, but since no slot has been connected to b's valueChanged() signal, nothing happens (the signal disappears).
呼叫a.setValue(79)回激發一個valueChanged()訊號,b會在它的setValue插槽中收到這個訊號,也就是b.setValue(79)被呼叫。然後,b會輪流激發同樣的valueChanged()訊號,但因為沒有插槽被連線到b的valueChanged()訊號,於是沒有訊號發生(訊號消失)。

Note that the setValue() function sets the value and emits the signal only if v != val. This prevents infinite looping in the case of cyclic connections (e.g. if b.valueChanged() were connected to a.setValue()).
注意setValue()函式為value賦值並激發訊號僅當v!=val。這避免了迴圈連線的情況下(例如:如果b.valueChanged()也被連線到了a.setValue())的無限迴圈。

This example illustrates that objects can work together without knowing each other, as long as there is someone around to set up a connection between them initially.
這個例子講解了物件可以在不知道對方的情況下協同工作,只要在他們初始化時為他們建立連線。

The preprocessor changes or removes the signals, slots and emit keywords so that the compiler is presented with standard C++.
前處理器改變或者去掉signals,slots和emit關鍵字,所以編譯器以標準C++來對待它。

Run the moc on class definitions that contain signals or slots. This produces a C++ source file which should be compiled and linked with the other object files for the application.
執行moc(Meta Object Compiler)來處理包含訊號或者插槽的類定義。這產生一個能夠被編譯並且與應用程式中其他目標檔案連線起來的C++原始碼。

Signals
訊號

Signals are emitted by an object when its internal state has changed in some way that might be interesting to the object's client or owner. Only the class that defines a signal and its subclasses can emit the signal.
當物件的客戶或者所有者所感興趣的物件內部狀態改變了,訊號就被激發。只有定義了訊號的類及其子類能夠激發訊號。

A list box, for example, emits both highlighted() and activated() signals. Most objects will probably only be interested in activated() but some may want to know about which item in the list box is currently highlighted. If the signal is interesting to two different objects you just connect the signal to slots in both objects.
一個列表框,既能激發heighlighed()訊號也能激發activated()訊號。大多數物件可能只對activated()感興趣,但有時希望知道列表框中的哪一個元素當前是高亮顯示的。如果訊號被兩個不同的物件所感興趣,你可以連線訊號到兩個物件的插槽。

When a signal is emitted, the slots connected to it are executed immediately, just like a normal function call. The signal/slot mechanism is totally independent of any GUI event loop. The emit will return when all slots have returned.
當訊號被激發了,連線到它的插槽會立即執行,就像普通的函式呼叫那樣。訊號/插槽機制完全獨立於任何GUI事件迴圈。當所有插槽返回了,emit才會返回。

If several slots are connected to one signal, the slots will be executed one after the other, in an arbitrary order, when the signal is emitted.
如果幾個插槽被連線到同一訊號,這些插槽會在訊號被激發的時候以任意秩序依次執行。

Signals are automatically generated by the moc and must not be implemented in the .cpp file. They can never have return types (i.e. use void).
訊號會自動被moc自動生成,不必在.cpp裡面實現。它們永遠不會返回值(也就是void)。

A note about arguments. Our experience shows that signals and slots are more reusable if they do not use special types. If QScrollBar::valueChanged() were to use a special type such as the hypothetical QRangeControl::Range, it could only be connected to slots designed specifically for QRangeControl. Something as simple as the program in Tutorial 5 would be impossible.
關於引數。我們的經驗顯示如果訊號與插槽沒有使用專門的型別,他們的可重用性會更高。如果QscrollBar::valueChanged()使用了一個指定型別,假設是QRangeControl::Range,它就只能被連線到明確到QRangeControl。事情和Tutorial5裡面一樣簡單是不可能的。

Slots
插槽
A slot is called when a signal connected to it is emitted. Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them. A slot's arguments cannot have default values, and, like signals, it is rarely wise to use your own custom types for slot arguments.
插槽會在鏈街道它的訊號被激發時呼叫。插槽是普通的C++函式能被普通的呼叫。

Since slots are normal member functions with just a little extra spice, they have access rights like ordinary member functions. A slot's access right determines who can connect to it:
因為插槽是略為擴充套件的普通成員函式,他們擁有普通成員函式的訪問許可權。插槽的訪問許可權決定了誰能連線它:

A public slots: section contains slots that anyone can connect signals to. This is very useful for component programming: you create objects that know nothing about each other, connect their signals and slots so that information is passed correctly, and, like a model railway, turn it on and leave it running.
一個public slots:段包含了誰都能夠連線的插槽。這對元件程式設計非常有用:你建立對對方一無所知的物件,連線它們的訊號與插槽以便資訊能夠正確傳遞,就像鐵路模型一樣,開啟它就可以讓它自己執行。

A protected slots: section contains slots that this class and its subclasses may connect signals to. This is intended for slots that are part of the class' implementation rather than its interface to the rest of the world.
一個protected slots:段包含了它自身及其子類可以連線的插槽。這是有意讓插槽成為類實現的一部分,而不是對外界的介面。

A private slots: section contains slots that only the class itself may connect signals to. This is intended for very tightly connected classes, where even subclasses aren't trusted to get the connections right.
一個private slots:段包含了只能被類自身連線的插槽。這有意對緊連線類,甚至子類也不能希望得到正確的連線。

You can also define slots to be virtual, which we have found quite useful in practice.
你也可以定義插槽為virtual的,我們發現這在實際中特別有用。

The signals and slots mechanism is efficient, but not quite as fast as "real" callbacks. Signals and slots are slightly slower because of the increased flexibility they provide, although the difference for real applications is insignificant. In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls. This is the overhead required to locate the connection object, to safely iterate over all connections (i.e. checking that subsequent receivers have not been destroyed during the emission) and to marshall any parameters in a generic fashion. While ten non-virtual function calls may sound like a lot, it's much less overhead than any 'new' or 'delete' operation, for example. As soon as you perform a string, vector or list operation that behind the scene requires 'new' or 'delete', the signals and slots overhead is only responsible for a very small proportion of the complete function call costs. The same is true whenever you do a system call in a slot - or indirectly call more than ten functions. On an i586-500, you can emit around 2,000,000 signals per second connected to one receiver, or around 1,200,000 per second connected to two receivers. The simplicity and flexibility of the signals and slots mechanism is well worth the overhead, which your users won't even notice.
訊號與插槽機制是有效率的,但不能完全和“真實”的回撥相比。訊號與插槽會因為它帶來了靈活性的增加而稍慢一些,儘管這些差別在真正的應用程式中無關緊要。通常,激發一個已連線到某些插槽的訊號會比直接呼叫接收者慢10倍,這裡假設沒有虛擬函式呼叫。這是定位連線物件所需要的,為了安全的迭代(遍歷)所有連線(也就是檢查下一個接收者在激發期間沒有被破壞)。也許10次非虛擬函式呼叫聽起來很多,但它遠少於’new’或者’delete’操作。比如,當你進行一個字串、向量或者列表操作,這需要隱藏的’new’或者’delete’,訊號與插槽只佔用了非常全部函式呼叫開銷的非常少的一部分。這對你在插槽裡進行系統呼叫或者間接呼叫10個以上的函式也是一樣的。在一臺i586-500上,你可以每秒激發大約2,000,000次已連線一個接收者上的訊號,或者大約1,200,000次已連線到兩個接收者上的訊號。訊號與插槽機制的簡單性和靈活性的開銷是值得的,甚至你的使用者不會發覺。

Meta Object Information
元物件資訊

The meta object compiler (moc) parses the class declaration in a C++ file and generates C++ code that initializes the meta object. The meta object contains names of all signal and slot members, as well as pointers to these functions. (For more information on Qt's Meta Object System, see Why doesn't Qt use templates for signals and slots?.)
元物件編譯器(moc)解析C++檔案中的類宣告並在初始化生成C++程式碼。元物件包含包含所有訊號和插槽成員的名字,就像這些函式指標一樣。(更多關於Qt 元物件系統的資訊,見Why doesn't Qt use templates for signals and slots?)

The meta object contains additional information such as the object's class name. You can also check if an object inherits a specific class, for example:
元物件包含像類名這樣的附加資訊,你也可以檢查一個物件是否繼承自一個指定的類,例如:
  if ( widget->inherits("QButton") ) {
        // yes, it is a push button, radio button etc.
  }

A Real Example
一個例子
Here is a simple commented example (code fragments from qlcdnumber.h ).
這有一個簡單的帶註釋的例子(qlcdnumber.h中的程式碼片斷)。
    #include "qframe.h"
    #include "qbitarray.h"

    class QLCDNumber : public QFrame

QLCDNumber inherits QObject, which has most of the signal/slot knowledge, via QFrame and QWidget, and #include's the relevant declarations.
QLCDNumber通過Qframe和Qwidget間接繼承自擁有最多訊號/插槽知識的Qobject,並且包含了相關宣告。
    {
        Q_OBJECT
Q_OBJECT is expanded by the preprocessor to declare several member functions that are implemented by the moc; if you get compiler errors along the lines of "virtual function QButton::className not defined" you have probably forgotten to run the moc or to include the moc output in the link command.
Q_OBJECT被前處理器擴充套件宣告來宣告幾個由moc實現的成員函式;如果你得到像這樣的一行編譯器(應該是連結器)錯誤”virtual function Qbutton::className not defined”,也許是你忘了執行moc或者忘了在連結命令包含moc輸出。
    public:
        QLCDNumber( QWidget *parent=0, const char *name=0 );
        QLCDNumber( uint numDigits, QWidget *parent=0, const char *name=0 );
It's not obviously relevant to the moc, but if you inherit QWidget you almost certainly want to have the parent and name arguments in your constructors, and pass them to the parent constructor.
這與moc沒有明顯的關係,但如果你繼承了Qwidget並確信希望在你的建構函式裡有parent和name引數,將它們傳入父類的建構函式。

Some destructors and member functions are omitted here; the moc ignores member functions.
一些解構函式和成員函式在這裡省略了;moc忽略了成員函式。
    signals:
        void    overflow();
QLCDNumber emits a signal when it is asked to show an impossible value.
QLCDNumber在被要求顯示不可能的值時會激發一個訊號。

If you don't care about overflow, or you know that overflow cannot occur, you can ignore the overflow() signal, i.e. don't connect it to any slot.
如果你不關心溢位,或者你知道溢位不可能發生,你可以忽略overflow()訊號,也就是不將它與任何插槽連線。

If, on the other hand, you want to call two different error functions when the number overflows, simply connect the signal to two different slots. Qt will call both (in arbitrary order).
另一方面,如果你希望當數字溢位時呼叫兩個不同的錯誤處理函式,簡單地將訊號與兩個不同的插槽連線。Qt兩個都會呼叫(以任意次序)。
    public slots:
        void    display( int num );
        void    display( double num );
        void    display( const char *str );
        void    setHexMode();
        void    setDecMode();
        void    setOctMode();
        void    setBinMode();
        void    smallDecimalPoint( bool );
A slot is a receiving function, used to get information about state changes in other widgets. QLCDNumber uses it, as the code above indicates, to set the displayed number. Since display() is part of the class' interface with the rest of the program, the slot is public.
插槽是一個接收函式用來取得其他部件(widgets)狀態改變的資訊。QLCDNumber使用了它,就像上面的程式碼展示的那樣,用來設定顯示數字。因為display()是類介面的一部分,所以slot是公有的。

Several of the example programs connect the newValue signal of a QScrollBar to the display slot, so the LCD number continuously shows the value of the scroll bar.
例程連線QscrollBar的newValue訊號到display插槽,所以LCD持續顯示滾動條的值。

Note that display() is overloaded; Qt will select the appropriate version when you connect a signal to the slot. With callbacks, you'd have to find five different names and keep track of the types yourself.
注意display()被過載了;當你連線一個訊號到這個插槽時,Qt會選擇適當的版本。而回調,你得找5個不同的名字並自己的來跟蹤型別。

Some irrelevant member functions have been omitted from this example.
一些不相關的成員函式從例子中被省略了。
    };

Copyright © 2001 Trolltech  Trademarks  Qt version 3.0.1 

---------------------------------------------------------------------------------------------------------------------------------------------------

 QT的 signal & slot機制
  signals和slots機制是QT的根本。
      slots和c++的成員函式(member function)幾乎一樣的,它們能定義為virtual,能overloaded,能定義為public,protected或private。能和c++其他成員函式一樣
  被直接呼叫,參量(paramters)能定義為任何型別,他與其它成員函式不同的是它能連線signal,當signal發射(emitted)時,它能自動呼叫。
      它們格式一般為:
         connect(sender,SIGNAL(signal),receiver,SLOT(slot));
   sender和receiver必需為指向一個QObject物件的指標。而signal和slot只需指出參量型別而不用寫出產量名稱。
        以下是一些可能發生的例子。
     a:一個signal能連線多個slots。
     例如:  connect(slider,SIGNAL(valueChange(int)),spinBox,SLOT(setValue(int)));
            connect(slider,SIGNAL(valueChange(int)),this,SLOT(updateValue(int)));
     b:多個signal能連線一個slots。
     connect(lcd, SIGNAL(overflow()),
        this, SLOT(handleMathError()));
     connect(calculator, SIGNAL(divisionByZero()),
        this, SLOT(handleMathError()));
     c:一個signal能連線其他signal。
     connect(lineEdit, SIGNAL(textChanged(const QString &)),
        this, SIGNAL(updateRecord(const QString &)));
     d:連線能夠刪除。
       disconnect(lcd, SIGNAL(overflow()),
           this, SLOT(handleMathError()));
     要成功連線signal到slot,必須要參量型別及數量相同。
       connect(ftp, SIGNAL(rawCommandReply(int, const QString &)),
        this, SLOT(processReply(int, const QString &)));

     但萬事無絕對,如果signal比slot參量多,多出參量可以忽略。
     例如:connect(ftp, SIGNAL(rawCommandReply(int, const QString &)),
         this, SLOT(checkErrorCode(int)));

相關推薦

Qt----Signals and Slots

譯者注:Qt是一套跨平臺的C++ GUI應用程式框架,最近非常流行。它的訊號與插槽(Signals and Slots)機制與Windows的訊息機制有很大差別,特此翻譯其手冊中這一篇,為學習亦為好奇,若錯誤百出還請指正為謝。 Signals and Slots Signal

Qt:訊號與槽(Signals and Slots) 下

http://hi.baidu.com/spygg/item/519f4dd7575704ce1b72b4e7 訊號(Signals) 只有定義了訊號的類及其子類可以發出訊號。 一個訊號發出後,連線的槽通常會立即執行,就像一個普通的函式呼叫。訊號與槽機制完全獨立於GUI的

CSS - and 佈局(浮動、流式、定位、相對、絕對、固定)

目錄 一、文件流(normal flow) 1、概念 2、BFC(Block formatting context 塊格式化上下文)  - 2.1一切皆為框  - 2.2 無名塊框 3、BFC規則 二、浮動佈局 1

Support for Signals and Slots

Support for Signals and Slots One of the key features of Qt is its use of signals and slots to communicate between objects. Their use encourages the

Qt工作筆記-Qt筆記-QPainter::drawText寫文字並且居中

官方解析 畫給定的text並且從指定的position。 這個函式不能處理換行符號(\n),不能將文字分割成多行,也不能分行展示。使用QPainer::drawText()的過載函式可以繪製一個多邊形

Qt閱讀筆記-Qt工作筆記QProcess::finished的解析與例項

目錄 官方解析 博主例子 官方解析 void QProcess::finished(int exitCode, QProcess::ExitStatus exitStatus) 這個訊號是當process完成時候才發射的。exitCode是程序退出的程式碼(

Qt閱讀筆記-編寫應用指令碼解析與例項

目錄 官方解析 博主例子 官方解析 編寫應用指令碼 Qt提供了JavaScript指令碼對應用程式的支援。下面將全方位說明JavaScript在Qt中的應用。 Scripting Class

Qt工作筆記-Qt閱讀筆記-QXmlStreamReader::qualifiedName()如何解析帶限制符的XML

目錄 官方解析 博主例子 官方解析 QXmlStreamReader::qualifiedName() 返回StartElement或EndElement的限制名; 限制名是XML資料中元素的原

Qt閱讀筆記-QPropertyAnimation官方解析及例項

目錄 官方解析 博主例子 官方解析 QPropertyAnimation QPropertyAnimation類為Qt屬性提供動畫。 QPropertyAnimation類可以修改Qt屬性,從

and 佈局(浮動、流式、定位、相對、絕對、固定)

目錄 1、概念 1、案例 一、文件流(normal flow) 1、概念   本質為normal flow(普通流、常規流)將窗體自上而下分成一行一行,塊級元素從上至下、行內元素在

Qt閱讀筆記-windowOpacity官方解析及例項(兩種方法使得程式漸變出現)

目錄 官方解析 博主例子 官方解析 windowOpacity 這個屬性控制視窗的透明度。 他的取值範圍是1.0(不透明)至0.0(全透明)。 預設情況下,這個屬性的值是1.0。 這個屬性在L

Qt閱讀筆記-Widgets Tutorial官方解析及例項

目錄 官方解析 博主栗子 官方解析 QWiget通常作為其他視窗的容器,可以使用QWidget到達最小代價定義自定義控制元件(經驗:通常可以採用子類QLabel做成自定義控制元件) 可以通過構

Qt閱讀筆記-QHeaderView::sectionResized官方解析與例項

官方解析 不翻譯了,很簡單的英語 博主例子 程式執行截圖如下 原始碼如下: widget.h #ifndef WIDGET_H #define WIDGET_H #includ

Qt閱讀筆記-QGraphicsItem::paint中QStyleOptionGraphicsItem *option的進一步認識

官方解析 painter : 此引數用於繪圖; option : 提供了item的風格,比如item的狀態,曝光度以及詳細的資訊; widget : 想畫到哪個widget上,如果要畫在快取區上,這個

Qt閱讀筆記-C++與QML混合程式設計(QML畫餅狀圖)

這裡只點名一點: Qt Charts是利用Qt的圖形檢視框架(QGraphics)搞出來的,底層並不是用OPenGL,而QML卻與OPenGL的底層緊密關聯 執行截圖如下: 原始碼如下

Qt閱讀筆記-最簡單的動態3D圓環例項

程式的邏輯如下: 正常顯示邏輯: 1.場景中要存在一個根實體; 2.為根實體載入材質; 3.在根實體下新增其他實體; 4.為其他實體新增額外的資料(比如畫圓環等); 5.放置攝像機,設定前景等屬性。 攝像機視覺方面的邏輯: 1.放置攝像機,設定前景等屬性; 2.建立攝

Qt閱讀筆記-qmake入門指南

目錄 qmake的作用 1.生成Makefile檔案; 2.生成moc和uic檔案; 3.用vs開啟Qt時無需要改變pro檔案就能能夠生成專案; 在Linux中qmake的主要目的是簡化程式設計師編寫Makefile; 簡單的小例子 手寫一

React官方--Lists and Keys

Lists and Keys 下面是JavaScript使用map()歷一個數組,在React中可以使用元素列表來實現相同操作。 //javascript take an array and double them const numbers = [1,

Qt譯:QPushButton

0.詳細介紹 QPushButton類提供了一個命令的按鈕.它可以說是圖形介面中最常用的控制元件之一了.點選按鈕來執行一些計算機的命令,或者回答一些問題.常用的按鈕有:OK, Apply, Cancel, Close, Yes, No and Help等.按鈕

2.2深入訊號和槽(Signals and Slots in Depth)

訊號和槽是Qt程式設計的一個重要部分。這個機制可以在物件之間彼此並不瞭解的情況下將它們的行為聯絡起來。在前幾個例程中,我們已經連線了訊號和槽,聲明瞭控制元件自己的訊號和槽,並實現了槽函式,傳送了訊號。現在來更深入瞭解這個機制。 槽和普通的c++成員函式很像。它們可以是虛擬函式