SIGNAL and SLOTS
-
I have QList<QPushButton > as this:
@
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
QPushButton _btnTemp = new QPushButton(_subWidget);
_btnTemp->setGeometry(i40, j 40, 40, 40);
_arrBtn->append(_btnTemp);
}
}
@
with
@_arrBtn = new QList<QPushButton *>();@
How i can catch signal, slot on each button?
I want when i click on any button, other buttons in cross with it will be changed color? how i can do?
Thanks for support!!!P/S:sry, my English not good!
-
simply connect all the buttons signal pressed() to one slot, and in response set the color:
@Xxx:init()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
QPushButton _btnTemp = new QPushButton(_subWidget);
connect(_btnTemp, SIGNAL(pressed()), this, SLOT(buttonPressed()));
_btnTemp->setGeometry(i40, j* 40, 40, 40);
_arrBtn->append(_btnTemp);
}
}
}XXx::buttonPressed()
{
QPushButton* btn_pressed = static_cast<QPushButton*>(sender());
int pos = _arrBtn->indexOf(btn_pressed);
}@ -
Or, you look into using [[doc:QSignalMapper]].
-
[quote author="BilbonSacquet" date="1321433222"]simply connect all the buttons signal pressed() to one slot, and in response set the color:
@Xxx:init()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
QPushButton _btnTemp = new QPushButton(_subWidget);
connect(_btnTemp, SIGNAL(pressed()), this, SLOT(buttonPressed()));
_btnTemp->setGeometry(i40, j* 40, 40, 40);
_arrBtn->append(_btnTemp);
}
}
}XXx::buttonPressed()
{
QPushButton* btn_pressed = static_cast<QPushButton*>(sender());
int pos = _arrBtn->indexOf(btn_pressed);
}@[/quote]Thanks for support! when i did as u say i have a problem:
my index in _arrbtn have shape:
0 1 2 3 4
5 6 7 8 9
....
when pos i taked have shape:
0 5 10 ....
1 6
2 7
3 8
4 9...
which way i can solve it? thanks u again :) -
On line 9 of BilbonSacquet's post, you might swap the use of i and j.
-
Yes, I just give the principle ... it's not a working solution.
The pos should reconstruct into i/j position: with % and /.
You could too derivate QPushButton to store the position, you could even think to create a slot changeColor() and connect directly the pressed() signal to the slot changeColor()....
To help you more I need a concrete implementation ... :) -
now, i changed QPushButton = QTextEdit. So how to catch Signal when i clicked on TextEdit i had created - not Signal(TextChanged);
thanks for support!P/s: sry because spam, i have just solved it :">
-
There is no signal sends only by click into a QTextEdit. Because you need a signal only if something change and of course by clicking in a text field make no change in the content (you should begin at least to select).
Now if you want anyway one, inherits the QTextEdit and overload the mousePressEvent() and send a signal from here.