Explanation on connect in Qt

I need a bit of an explanation about the connect function and his use. I already connect signal and QButton or signal and timer.

Now I create my own signal, this signal is emit when I receive data from a client. I want to connect this signal to a QTableWidget ... But there's no QButton or something like that, so I don't know how to call connect() , and I realize I don't really understand the function.

Can you explain me how to connect my QTableWidget and my signal ?

Answers


Look at the QTableWidget docs, for example via Qt Assistant or this link:

http://qt-project.org/doc/qt-4.8/qtablewidget.html

(note that this is for Qt 4.8, but docs for other versions are also available online). Search for the section entitled 'Signals' - these are the signals that every QTableWidget will emit in certain situations. Each Qt widget has such a set of signals characteristic for this particular widget. The signals for QTableWidget are different than for QPushButton you've mentioned.

For example, QPushButton has the signal clicked() that is emitted when the button gets pushed, but it is only one of the several signals it can emit. It can emit signals also e.g. when the title of the main window gets changed. QTableWidget doesn't feature the clicked() signal, but has many others.

Find the signal that will enable you to do what you want in your program, and then connect it to the slot via the connect() function. There is a bunch of readymade slots available for QTableWidget (search for the section 'Slots' in the above mentioned docs).

If you cannot find the right slot, you can write one yourself and connect signal to it. If you cannot find proper signal, you may have to subclass QTableWidget and add an appropriate function to it that will emit the signal in the situation defined by you.

Also, here's a quick guide to how the connect() function works in general:

http://qt-project.org/doc/qt-4.8/signalsandslots.html

BTW: always state the Qt version you use since some problems are version-specific.


The connect() method adds the tuple (sender, sender signal, receiver, receiver signal) to the connection table for a given sender. Both the sender and the receiver are pointers to instances of QObject. The signal and slot can be provided as signatures wrapped in SIGNAL or SLOT macros, or as method pointers in Qt 5. Internally they are stored as method indices.

You need to know the contents of this tuple in order to call connect(). That's all.

The connection table is used within the signal to invoke the slots. A signal is a method whose implementation is generated by the moc tool. The implementation iterates through the connection table and invokes the slots connected to the signal.


"when I receive data from a client. I want to connect this signal to a QTableWidget"

This is ambiguous. Yes, there are no buttons in a QTableWidget. A QTableWidget is defined in the Qt documentation as follows: -

The QTableWidget class provides an item-based table view with a default model.

If you look at the public slots, you can see what your signals can connect to in the QTableWidget class.

However, I expect that what you're wanting to do is add data to the table, having received it from a data source. As the QTableWidget class is a view on a model, it's the model you need to look at here.

I suggest you begin by reading up on Qt's Model-View programming, to understand how models link to views and how to populate a model with your received data.

Also, if you're not quite sure about how signals and slot work, you can read the Qt tutorial for that here.


In order to to what you want, you need to connect a signal to a slot, and call the slot when the signal is emitted. There's no reason to use only QButton. You can use every class in order to emit right signals (with right arguments).

You need to use connect connecting your signal and the QTableWidget slot.


Need Your Help

iPhone screenshot

iphone screenshot

How can I create a iPhone screenshot? When I open Xcode I go to Window>Organizer but there is no Devices tab visible. I have a jail-braken phone with OS 3.1.2.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.