PDA

View Full Version : QFrame MousePressedEvent



jwflammer
1st March 2011, 23:11
I have a 10 X 10 square QFrame grid. my goal is to have a button to be checked for what color you want and then mouse over the frame and click and the QFrames background changes to the color that was selected.


void matrix::REDBTN()
{
if (ui->REDbtn->isChecked())
{
ui->GREENbtn->setChecked(false);
ui->BLUEbtn->setChecked(false);
ui->WHITEbtn->setChecked(false);
ui->PURPLEbtn->setChecked(false);
ui->YELLOWbtn->setChecked(false);
ui->CYANbtn->setChecked(false);
if (ui->REDbtn->isChecked())
{
ui->box_0_0->mousePressEvent(setStyleSheet("background-color: rgb(255,0,0)"));
}
}
} i get an error when using this code. what am i doing wrong?

high_flyer
2nd March 2011, 10:06
if (ui->REDbtn->isChecked())
{
ui->box_0_0->mousePressEvent(setStyleSheet("background-color: rgb(255,0,0)"));
}



what is this code supposed to do?
mousePressEvent is an event handler, do you know what that means?
Have you looked at the mousePressEvent() doc, to see which parameters it takes?
mousePressEvent() is not supposed to be called like that directly.

jwflammer
2nd March 2011, 22:15
i have and im lost on what im suppose to do it seems that i have to make mousePressEvent a void function and that works but i want it to be able to click on a box and if red button is checked make that box red.


void matrix::mousePressEvent(QMouseEvent *event)
{
QFrame *frm = findChild<QFrame*>(QString("box_%2_%2").arg(event));
if (frm)
{
frm->setStyleSheet("background-color: rgb(255,0,0)");

}
}

high_flyer
3rd March 2011, 10:54
Look, what you have said so far, and your code, suggest you don't know C++.
C++ basic topics are off topic for this forum.

jwflammer
4th March 2011, 01:48
I do know C++! although i did not take a class on c++, its all self thought. If my labelling of things is wrong let me know. Don't assume i don't know some thing, i have gotten his far with QT and have made some fun useless apps. look here is my hole project in a zip. help me out don't bash. LED_Matrix.zip (freewaypickup.com/QTprojects/LED_Matrix.zip)

norobro
4th March 2011, 05:15
Consider putting your frames in a QTableWidget. It would simplify your code.
Something like:
...
tw = new QTableWidget(10,10,this);
for(int row=0;row<10;++row)
for(int col=0;col<10;++col){
QFrame *frame = new QFrame;
frame->setMaximumSize(50,50);
frame->setMinimumSize(50,50);
frame->setStyleSheet(//set color, corner radius etc//);
tableWidget->setCellWidget(row,col,frame);
}
tableWidget->resizeColumnsToContents();
tableWidget->resizeRowsToContents();
connect(tableWidget,SIGNAL(cellClicked(int,int)),t his,SLOT(yourSlot(int,int)));
...

Then in the slot change the color of the frame in the appropriate cell, according to which color button is checked, with QTableWidget->cellWidget(int, int). As an aside, a QComboBox might work better than QPushButtons for color selection.
There might be a way to change all of the cells to the same color using style sheets but I think you will have to iterate over them to change the color.

Wong
4th March 2011, 12:05
use QButtonGroup....
And color button has a id.
QButtonGroup::addButton ( QAbstractButton * button, int id )

connect(buttonGroup, SIGNAL(buttonClicked ( int id )), frame, SLOT(setId(int id)));

In your QFrame subclass, you can add a member int id
void matrix::setId(int id)
{
this->id = id;
}

void matrix::mousePressEvent(QMouseEvent *event)
{
switch(id)
{
case 0: set red; break;
case 1: set blue; break;
.....
}
}