[solved] Please, help me understand how to make communication between windows
-
Hi everyone!
I'm new in Qt programming and I stuck on basic function.
As everybody I would like to make application which use different windows for different actions. My target is to separate some windows into different threads but before I jump to threading I can't solve easier problem. How to exchange data between two or more windows?
I have analyzed many examples of signals and slots but most of them refered to objects on one window or were total magic and I still can't get it.
I have prepared simply app to present my problem and - I hope - to get the most helpful answers.
!http://shimano.one.pl/qtmultiwin.png(Communication between windows in Qt)!
Picture legend:
1 - QListView named fruitsListView
2 - QPushButton named addFruitButton - executing "A" (Open "Add fruit" window)
3 - QMainWindow named merchandise4 - QPushButton named saveFruitButton - SHOULD execute "B" (Send form data to "merchandise" window and close "Add fruit" window)
5 - QDialog named addFruits
6 - QRadioButton named radioButton
7 - QRadioButton named radioButton_2
8 - QLineEdit named fruitNameEdit
9 - QLineEedit named fruitPriceEditA - Open "5":
@
void merchandise::on_addFruitButton_clicked()
{
addFruits newFruit;
newFruit.setModal(true);
newFruit.exec();
}
@B - Should use data from "6, 7, 8 , 9" to execute void addFruit() which should insert new position to "1"
Basic questions:
How can I send data from "Add fruit" to "merchandise" and why I should use slots and signals to do that?
How exactly signals work? What signals should be used in this example and where their code should be placed (which classes)?
Are there any other methods to communicate windows? Which method is the most attractive (including slots and signals)?
-
Well... You use signals and slots... In addFruits you connect the Ok button to a slot (let's say sendDataToOtherWindow) in which you emit the signal (let's say fruitSignal) with the data you want... You connect fruitSignal to a slot in the merchandise class (let's say fruitReceiver) and operate on your data... You should use signals and slots because it is the easiest way...
So... To conclude... You have something like this:
@ In addFruits you have:
connect(okButton, SIGNAL(clicked()), addFruits, SLOT(sendDataToOtherWindow())); // Instead of addFruits you put this. addFruits is the class in which you connect them.void addFruits::sendDataToOtherWindow() {
emit fruitSignal(QString, int, etc); // here you emit the signal with the data you want... what ever data, just remember that your connected SLOT must have the same parameters
}// In merchandise you have:
connect(addFruitsObject, SIGNAL(fruitSignal(QString, int, etc), this, SLOT(fruitReceiver(QString, int, etc)));void merchandise::fruitReceiver(QString a, int b, etc) {
// Add them to your lists
}
@ -
Hi,
some tips:
- Only One Thread can access to GUI
- Instead of use different Widget, I suggest to implement addFruits as QDialog subclass
As general rule I suggest to study how signal/slot works
-
[quote author="b1gsnak3" date="1364304128"]
@
// In merchandise you have:
connect(addFruitsObject, SIGNAL(fruitSignal(QString, int, etc), this, SLOT(fruitReceiver(QString, int, etc)));
@[/quote]
Can you explain me, what should I use as "addFruitsObject"? I tried write there name of "Add fruits" window's class (exactly: AddFruits) but it doesn't work. Of course I can't even use saveFruitButton, so I don't get it, how to receive that signal?
Edit:
One more question - Do I have to define own signals (same like slots in section private/public slots) or just type emit myOwnSignal(blah, blah)? -
Per favore, leggete Documentation!
in connessione è NECESSARIO utilizzare Object * * Nome e Classe non * Nome *
E 'molto difficile da spiegare segnale / meccanismo di slot in poche righe.
Leggi documentazione Qt, provate con semplice esempio e dopo andare al più difficili problemi. -
Well you should create a object of the class and use that in your connect... That's why I used addFruitsObject... It is a instantiation of the AddFruits class...
For your other question... Yes you do... But in your class definition you must have the Q_OBJECT macro, and then declare your signals using the "signals:" key... Just like private or public, only that it is only called signals no other identifiers... Also it would help if you first looked at some examples before jumping head first into developing an application using Qt... It took me some time to understand the signal/slot mechanism...
To conclude... For your AddFruits class you would have something like:
@
class AddFruits : public QDialog {
Q_OBJECT
public:
AddFruits(); // Constructorsignals:
void someSignal();public slots:
void someSlot();private:
QString someVariable;
}
@ -
[quote author="mcosta" date="1364324173"]Per favore, leggete Documentation! in connessione è NECESSARIO utilizzare Object * * Nome e Classe non * Nome * E 'molto difficile da spiegare segnale / meccanismo di slot in poche righe. Leggi documentazione Qt, provate con semplice esempio e dopo andare al più difficili problemi.[/quote]
I'm Sorry, I wrote in Italian.
Please read Qt Docs beacause isn't easy to explain signal/slot mechanism in few lines.
I suggesto to start with simple tutorial and then move to harder problems.However in connect() you must use object and not class
-
OK, I think, I got it!
I understood the idea of signals and slots mechanism... Even my app is working;) Thank you guys!