Results 1 to 8 of 8

Thread: A lot of checkeable images

  1. #1
    Join Date
    Jun 2009
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default A lot of checkeable images


    Hello,
    I'm creating a window with a lot of images but although I know how to add one I have problems adding the rest of them.
    I have done a class to insert the first image.
    Do I have to another class in the same. h and .cpp files?Itake many errors if I do like this! this is the class that I'm doing:


    changingbutton::changingbutton(QWidget *parent)
    : QPushButton(parent)

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: A lot of checkeable images

    hm, why do you subclass QPushButton?
    the fastes way it's to put a layout on your widget (for exmample QGridLayout) and then in a loop add a label (QLabel) with image into layout.

    another way, it's to create you own widget which will load images and draw it in its paintEvent.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jun 2009
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A lot of checkeable images

    I don't use a label because the image must behave as a button. When I push it, I have to see one picture and when i do not, another one.
    At first I tried doing with a label, but I didn't get my goal.
    Could you tell me how can i do the same with a label?

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: A lot of checkeable images

    the first option is to subclass QLabel and add signal clicked.
    the second otpion is to install event filter for a label and process QMouseEvent.

    but if you want to use buttons then I don't understand why you sublass QPushButton if you can use clicked signal for changing images.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: A lot of checkeable images

    take a look at this example (it can be optimazed, cashing imagez ect.), it's just for demonstration a buttons approach
    test.h
    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4. #include <QDialog>
    5.  
    6. class Test: public QDialog
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Test(QWidget *parent = 0);
    12.  
    13. private:
    14. void init();
    15.  
    16. private slots:
    17. void changeImage();
    18.  
    19. private:
    20. enum State { Background, Foreground };
    21.  
    22. int m_rows;
    23. int m_columns;
    24. class QGridLayout *m_gl;
    25. };
    26.  
    27. #endif//TEST_H
    To copy to clipboard, switch view to plain text mode 
    test.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "test.h"
    3.  
    4. Test::Test(QWidget *parent)
    5. : QDialog(parent), m_rows(5), m_columns(5)
    6. {
    7. m_gl = new QGridLayout(this);
    8. init();
    9. }
    10.  
    11. void Test::init()
    12. {
    13. for (int r = 0; r < m_rows; ++r) {
    14. for (int c = 0; c < m_columns; ++c) {
    15. QPushButton *pb = new QPushButton();
    16. pb->setProperty("state", Background);
    17. const QString bin = QString::number(r) + QString::number(c) + "b.png";
    18. const QString fin = QString::number(r) + QString::number(c) + "f.png";
    19.  
    20. QPixmap fp(10, 10);
    21. fp.fill(QColor(qrand()%255, qrand()%255, qrand()%255));
    22. fp.save(fin);//must be optimized
    23.  
    24. QPixmap bp(10, 10);
    25. bp.fill(QColor(qrand()%255, qrand()%255, qrand()%255));
    26. bp.save(bin);//must be optimized
    27.  
    28. pb->setProperty("backgroundImageName", bin);
    29. pb->setProperty("foregroundImageName", fin);
    30. pb->setText("Background");
    31. pb->setIcon(QIcon(bp));
    32. connect(pb, SIGNAL(clicked()), SLOT(changeImage()));
    33. m_gl->addWidget(pb, r, c);
    34. }
    35. }
    36. }
    37.  
    38. void Test::changeImage()
    39. {
    40. QPushButton *pb = qobject_cast<QPushButton *>(sender());
    41. if (!pb)
    42. return;
    43. const State state = State(pb->property("state").toInt());
    44. pb->setText(state == Background ? "Foreground" : "Background");
    45. pb->setProperty("state", state == Background ? Foreground : Background);
    46. const QString fileName = (state == Background ? pb->property("foregroundImageName").toString() : pb->property("backgroundImageName").toString());
    47. pb->setIcon(QIcon(fileName));
    48. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "test.h"
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. Test test;
    9. test.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by spirit; 17th June 2009 at 10:25.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Jun 2009
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A lot of checkeable images

    The code that I'm using to disabled a button is the following one:

    QPushButton *xxx= new QPushButton;
    xxx->setDisabled(true);
    xxx->setIcon(QIcon("IMAGE1.bmp"));
    xxx->setText("disabled");

    How can I disabled a button?If I do like this i don't lock the function of the button.

  7. #7
    Join Date
    Jun 2009
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A lot of checkeable images

    The code that I'm using to disabled a button is the following one:

    QPushButton *xxx= new QPushButton;
    xxx->setDisabled(true);
    xxx->setIcon(QIcon("IMAGE1.bmp"));
    xxx->setText("disabled");

    How can I disabled a button?If I do like this i don't lock the function of the button.

  8. #8
    Join Date
    Jun 2009
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Arrow A lot of checkeable images

    To disabled a button I only need to put this sentence:
    setDisabled(true).
    I have asked a stupid question!

    know I have another problem. My images are .bmp. I have loaded them to my new proyect giving a new name as you did "00b.qrc"... and then, in the place that you set in note of test.cpp:


    const QString bin = QString::number(r) + QString::number(c) + "b.png";
    const QString fin = QString::number(r) + QString::number(c) + "f.png";

    I change for :

    const QString bin = QString::number(r) + QString::number(c) + "b.bmp";
    const QString fin = QString::number(r) + QString::number(c) + "f.bmp";

    What I'm doing wrong?

Similar Threads

  1. Loading corrupt jpeg images with QImage
    By mikeee7 in forum Qt Programming
    Replies: 15
    Last Post: 3rd December 2010, 01:59
  2. QScrollArea not displaying large number of images
    By lpkincaid in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2009, 09:58
  3. Replies: 8
    Last Post: 8th May 2009, 09:14
  4. import large number of images
    By sriluyarlagadda in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2008, 10:26
  5. JPEG Images not shown in QiconView using QT3??
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 4th August 2006, 20:34

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.