Results 1 to 7 of 7

Thread: QFrame MousePressedEvent

  1. #1
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QFrame MousePressedEvent

    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.

    Qt Code:
    1. void matrix::REDBTN()
    2. {
    3. if (ui->REDbtn->isChecked())
    4. {
    5. ui->GREENbtn->setChecked(false);
    6. ui->BLUEbtn->setChecked(false);
    7. ui->WHITEbtn->setChecked(false);
    8. ui->PURPLEbtn->setChecked(false);
    9. ui->YELLOWbtn->setChecked(false);
    10. ui->CYANbtn->setChecked(false);
    11. if (ui->REDbtn->isChecked())
    12. {
    13. ui->box_0_0->mousePressEvent(setStyleSheet("background-color: rgb(255,0,0)"));
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    i get an error when using this code. what am i doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFrame MousePressedEvent

    Qt Code:
    1. if (ui->REDbtn->isChecked())
    2. {
    3. ui->box_0_0->mousePressEvent(setStyleSheet("background-color: rgb(255,0,0)"));
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFrame MousePressedEvent

    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.

    Qt Code:
    1. void matrix::mousePressEvent(QMouseEvent *event)
    2. {
    3. QFrame *frm = findChild<QFrame*>(QString("box_%2_%2").arg(event));
    4. if (frm)
    5. {
    6. frm->setStyleSheet("background-color: rgb(255,0,0)");
    7.  
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFrame MousePressedEvent

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFrame MousePressedEvent

    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

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QFrame MousePressedEvent

    Consider putting your frames in a QTableWidget. It would simplify your code.
    Something like:
    Qt Code:
    1. ...
    2. tw = new QTableWidget(10,10,this);
    3. for(int row=0;row<10;++row)
    4. for(int col=0;col<10;++col){
    5. QFrame *frame = new QFrame;
    6. frame->setMaximumSize(50,50);
    7. frame->setMinimumSize(50,50);
    8. frame->setStyleSheet(//set color, corner radius etc//);
    9. tableWidget->setCellWidget(row,col,frame);
    10. }
    11. tableWidget->resizeColumnsToContents();
    12. tableWidget->resizeRowsToContents();
    13. connect(tableWidget,SIGNAL(cellClicked(int,int)),this,SLOT(yourSlot(int,int)));
    14. ...
    To copy to clipboard, switch view to plain text mode 
    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.

  7. #7
    Join Date
    Dec 2010
    Posts
    33
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QFrame MousePressedEvent

    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;
    .....
    }
    }

Similar Threads

  1. Get QImage from QFrame
    By sheeeng in forum Qt Programming
    Replies: 8
    Last Post: 25th January 2010, 11:07
  2. QFrame V and H Line
    By bunjee in forum Qt Programming
    Replies: 5
    Last Post: 5th September 2008, 20:30
  3. Using css for all QFrame's except one.
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 10th July 2008, 09:05
  4. scrollbar in a qframe
    By davea402 in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2008, 19:26
  5. Reg - QFrame
    By suresh in forum Qt Programming
    Replies: 1
    Last Post: 1st November 2006, 05:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.