Results 1 to 15 of 15

Thread: Button grid with label rotate 90 degree

  1. #1
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Button grid with label rotate 90 degree

    I would like to build the follow grid with the qt designer:



    Now i have two problems:

    1.) How can i rotate a label 90 degree?
    2.) How can i build a button grid. If a button compressed should be a cross indicated and only one button can be pressed?

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Button grid with label rotate 90 degree

    1.) How can i rotate a label 90 degree?
    You have to subclass QLabel and do the painting yourself.
    2.) How can i build a button grid. If a button compressed should be a cross indicated and only one button can be pressed?
    use QGridLayout with the buttons and setAutoExclusive to them. For the cross on the buttom a) subclass and do the painting or b) use style sheets ": on" and ": off"
    Last edited by Lykurg; 19th June 2009 at 15:21. Reason: reformatted to look better

  3. #3
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Button grid with label rotate 90 degree

    You have to subclass QLabel and do the painting yourself
    OK, i try it.

    use QGridLayout with the buttons and setAutoExclusive to them. For the cross on the buttom a) subclass and do the painting or b) use style sheets ": on" and ": off"
    I think a QGridLayout with buttons looks like dirty. You know a qt widget as MSFlexGrid or can i use a tablewidget? If your answer is yes how can i customize the tablewidge for the right look?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Button grid with label rotate 90 degree

    Optical there is no difference if you use a QGridLayout or a QTableView, but QGridLayout is more easier.
    (Quick designer work with grid layout and buttons look like your screenshot)
    Attached Images Attached Images

  5. #5
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Button grid with label rotate 90 degree

    Optical there is no difference if you use a QGridLayout or a QTableView, but QGridLayout is more easier.
    (Quick designer work with grid layout and buttons look like your screenshot)
    Thank you, the screenshot looks good. I can set a cross(image) or color with css (QPushButton:checked{ background-color: red;}) and can controlling the button state with checkable and autoExclusive. But i can't get a look for my QGridLayout like your screenshot.

    I have attached my form, please can you modified the form for my?

    How can i query or set the state for checked button. I mine a function like setPushButtonChecked( int row, int column ). I need it to save and restore the user input.

    Thanks in advanced
    Attached Files Attached Files

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Button grid with label rotate 90 degree

    I would create a completely custom widget for this...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Button grid with label rotate 90 degree

    I would create a completely custom widget for this...
    OK, and how can i do that? How looks that widget?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Button grid with label rotate 90 degree

    It looks how you implement it to look. Just subclass QWidget and implement what you need. Drawing is simple, handling mouse should be easy as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Button grid with label rotate 90 degree

    You can also have a look at the Qxt library:

    Push button with vertical text:
    http://doc.libqxt.org/0.5.0/classQxtPushButton.html

    Download it from here: http://libqxt.org/

  10. #10
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Button grid with label rotate 90 degree

    Hi,

    thank you, Qxt library is nice.

    I know how can i subclass QWidget and rotate a label or button 90 degrees.

    Im not sure what widget i can implement to bulid a grid of buttons and query the row or column for the checked button. You mine i should draw my own grid? I think if i draw a grid i don't have the functionality of a button. Is it right?

    Can you tell me ho can i do that?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Button grid with label rotate 90 degree

    A functionality of the button comes down to clicking that you can handle in three lines of code by reimplementing mouseReleaseEvent().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Button grid with label rotate 90 degree

    A functionality of the button comes down to clicking that you can handle in three lines of code by reimplementing mouseReleaseEvent().
    I can reimplement mouseReleaseEvent(), but is a little bit complex for me to paint a grid of buttons that have a different state and only one butten can be pressed. You know a example for that?

    If you don't have a example i have another idea, i subclass a QGraphicsItem and add one QButton. This GraphicItem would add to a QGraphicsScene and display in a QGraphicsView. You think that is OK too?

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Button grid with label rotate 90 degree

    Quote Originally Posted by whitefurrows View Post
    You think that is OK too?
    If you want the total overkill, then yes

  14. #14
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Button grid with label rotate 90 degree

    I have try to paint a button grid and rotate a label.

    To rotate the label i have subclass a QLabel and rotate the coordinate system the given angle clockwise 270 degree in the paintEvent(). The label text was rotated but not paint on the right place on the label.

    To paint a button grid i have subclass a QWidget an reimplement the paint paintEvent() and mousePressEvent(). The grid looks dirty, code a function to query the row or column is difficult and the grid size is less than real size. The real size circa 15 x 15.

    I have attached a example. Can you help me to make it professionally, please?
    Attached Files Attached Files

  15. #15
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Button grid with label rotate 90 degree

    Please can you help me, write a function to query or set the state for checked button? It would be nice if i can build the grid a little bit dynamic in size!

    I have subclass a QWidget to build a button grid. I build the grid like this:
    Qt Code:
    1. QGridLayout* layout = new QGridLayout(this);
    2. layout->setMargin(3);
    3. layout->setSpacing(0);
    4.  
    5. // Create cells
    6. for (int i = 0; i < 9; ++i) {
    7. int col = (i % 3) * 3;
    8. int max_col = col + 3;
    9. int row = (i / 3) * 3;
    10. int max_row = row + 3;
    11.  
    12. box->setMargin(2);
    13. box->setSpacing(1);
    14. layout->addLayout(box, row / 3, col / 3);
    15.  
    16. for (int r = row; r < max_row; ++r) {
    17. for (int c = col; c < max_col; ++c) {
    18. //Cell* cell = new Cell(c, r, this, this);
    19. QPushButton* cell = new QPushButton(this);
    20. cell->setCheckable(true);
    21. cell->setAutoExclusive(true);
    22. box->addWidget(cell, r - row, c - col);
    23. m_cells[c][r] = cell;
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    Here is my solution for all other user to rotate a QLabel:

    myLabel.h
    Qt Code:
    1. #ifndef MYLABEL_H
    2. #define MYLABEL_H
    3.  
    4. #include <QLabel>
    5.  
    6. class MyLabel : public QLabel{
    7. Q_OBJECT
    8.  
    9. public:
    10. MyLabel(QWidget *parent = 0);
    11. ~MyLabel();
    12.  
    13. bool rotateText(float degrees);
    14.  
    15. private:
    16. float rotation;
    17.  
    18. protected:
    19. void paintEvent(QPaintEvent *);
    20. void drawRotatedText(QPainter *painter, float degrees, int x, int y, const QString &text);
    21. };
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 

    myLabel.cpp:
    Qt Code:
    1. #include "myLabel.h"
    2. #include <QPainter>
    3.  
    4. MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
    5. {
    6. this->rotateText(NULL);
    7. }
    8.  
    9. bool MyLabel::rotateText(float degrees)
    10. {
    11. if (degrees >=0 && degrees <= 360)
    12. {
    13. rotation=degrees;
    14. update();
    15. return TRUE;
    16. }
    17. return FALSE;
    18. }
    19.  
    20. void MyLabel::paintEvent(QPaintEvent *)
    21. {
    22. p.begin( this );
    23. p.setFont(font());
    24. drawRotatedText(&p, rotation, width() / 2, height() / 2, text());
    25. p.end();
    26. }
    27.  
    28. void MyLabel::drawRotatedText(QPainter *painter, float degrees, int x, int y, const QString &text)
    29. {
    30. painter->save();
    31. painter->translate(x, y);
    32. painter->rotate(degrees);
    33. painter->drawText(0, 0, text);
    34. painter->restore();
    35. }
    To copy to clipboard, switch view to plain text mode 

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.