Results 1 to 2 of 2

Thread: Please Help with QButton

  1. #1
    Join Date
    Nov 2011
    Posts
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Please Help with QButton

    I just want to create a button that will allow me to paint on an image i already have made in my main class. I want this button to draw a square on my image, but whenever i try and use it i cant have it redrawn on my label.

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3. #include <QDebug>
    4. #include <CoordinateValue.h>
    5. #include <mainwindow.h>
    6. #include <Level.h>
    7. //#include <Coordinate.h>
    8.  
    9. //#include<Graphics.h>
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QApplication app(argc, argv);
    14.  
    15. QLabel window;
    16. QPixmap lvlone;
    17. QPixmap ftowpic;
    18. QIcon ftowicon;
    19.  
    20. lvlone.load(QString::fromUtf8("C:/Users/Agup/Desktop/Map1.bmp"));
    21. ftowpic.load(QString::fromUtf8("C:/Users/Agup/Desktop/FreshmenTower.bmp"));
    22.  
    23. CoordinateValue cv;
    24. //cv.setValid(1);
    25.  
    26. //load pixmap into icon for button
    27. ftowicon.addPixmap(ftowpic);
    28. QSize ftowsize(25,25);
    29.  
    30.  
    31. // next code is to check if worked or not
    32. //qDebug() << goodLoad << lvlone.isNull() << lvlone.size() << lvlone.depth();
    33.  
    34. //make mappaint use the map as background
    35.  
    36.  
    37. QPainter mappaint(&lvlone);
    38.  
    39.  
    40. QPen Black((QColor::QColor(0,0,0)),1);
    41. mappaint.setPen(Black);
    42.  
    43. for(int i = 0;i<501;i += 50)
    44. {
    45. mappaint.drawLine(0,i,500,i);
    46. }
    47. for(int j = 0;j<501;j += 50)
    48. {
    49. mappaint.drawLine(j,0,j,500);
    50. }
    51. //use this code to draw towers depenrding on valid spots.
    52. //mappaint.drawPixmap(200,150,50,50, ftowpic);
    53.  
    54.  
    55.  
    56. //QPushButton Start("Start", &window);
    57. //Start.setGeometry(200,200,50,50);
    58. //QObject::connect(&Start,SIGNAL(clicked()),g,SLOT(PaintIT()));
    59.  
    60.  
    61. //freshmen tower button
    62. QPushButton FTower(ftowicon,"Freshmen\n Tower", &window);
    63. FTower.setFont(QFont("Purisa",8));
    64. FTower.setGeometry(500,0,100,100);
    65. FTower.setIconSize(ftowsize);
    66.  
    67.  
    68.  
    69. cv.getPainter(mappaint);
    70.  
    71. QObject::connect(&FTower,SIGNAL(clicked()),&cv, SLOT(paintValid()));
    72. QPushButton quit("Quit", &window);
    73. quit.setFont(QFont("Purisa", 12));
    74. quit.setGeometry(575,400,100,50);
    75. QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
    76.  
    77. window.resize(750,500);
    78. window.setPixmap(lvlone);
    79. window.show();
    80.  
    81. cv.paintValid();
    82. return app.exec();
    83. }
    To copy to clipboard, switch view to plain text mode 

    and it uses a slot from here

    Qt Code:
    1. #ifndef COORDINATEVALUE_H
    2. #define COORDINATEVALUE_H
    3.  
    4.  
    5. #include <QtCore>
    6. #include <QtGui>
    7. #include <QDebug>
    8. #include <Coordinate.h>
    9. class CoordinateValue: public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. Coordinate coor[100];
    14.  
    15. public:
    16. QPixmap newImage;
    17. QLabel newWindow;
    18. QPainter plzwrk;
    19.  
    20. //makes 100 coordinates
    21.  
    22. int getSpot(int x,int y)
    23. {
    24.  
    25. //returns numerical spot corresponding
    26. //with array coor for which spot coordinate is in
    27. return (x%50)+((y/50)*10);
    28. }
    29. void setValues()
    30. {
    31. //sets each value of grid in image
    32. //to a value of 1-100 based on location
    33. int c = 0;
    34. for(int i = 0; i<500; i+= 50){
    35. for(int j = 0;j<500; j+=50)
    36. {
    37. coor[c].setX(j);
    38. coor[c].setY(i);
    39. c++;
    40. }
    41. }
    42. }
    43. //get X Drawing Coordinates
    44. int getXDC(int spot)
    45. {
    46. return coor[spot].getX();
    47. }
    48. //get Y Drawing Coordinates
    49. int getYDC(int spot)
    50. {
    51. return coor[spot].getY();
    52. }
    53. void setValid(int levelnum)
    54. {
    55. // switch (levelnum)
    56. // case 1:
    57.  
    58. // break;
    59. // case 2:
    60.  
    61. // break;
    62.  
    63. }
    64. void getPainter(QPainter &painter)
    65. {
    66. plzwrk = painter;
    67. }
    68. public slots:
    69. void paintValid()
    70. {
    71. QPen linepen;
    72. linepen.setWidth(2);
    73. linepen.setColor(Qt::green);
    74. plzwrk.setPen(linepen);
    75.  
    76. QPolygon poly;
    77. poly<< QPoint(0,0);
    78. poly<< QPoint(0,50);
    79. poly<< QPoint(50,50);
    80. poly<< QPoint(50,0);
    81.  
    82. QBrush transbrush;
    83. transbrush.setColor(Qt::green);
    84. transbrush.setStyle(Qt::Dense5Pattern);
    85.  
    86. path.addPolygon(poly);
    87.  
    88. plzwrk.drawPolygon(poly);
    89. plzwrk.fillPath(path,transbrush);
    90.  
    91. }
    92. void doNothing()
    93. {
    94. QString new1 = "hi";
    95. qDebug()<<new1;
    96. }
    97.  
    98. };
    99. #endif // COORDINATEVALUE_H
    To copy to clipboard, switch view to plain text mode 






    i get that its not neat but any help would greatly be appreciated thanks

  2. #2
    Join Date
    Apr 2010
    Location
    Russian Federation, Kaluga
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Please Help with QButton

    Quote Originally Posted by agup006 View Post
    I just want to create a button that will allow me to paint on an image
    For this task you do not necessarily create you own button's class. You can you QPushButton or QToolButton.
    See http://doc.qt.nokia.com/stable/qabst...html#icon-prop.

    P.S. - I don't saw your code - sorry.

Similar Threads

  1. Replies: 2
    Last Post: 28th April 2011, 19:21
  2. Replies: 0
    Last Post: 10th February 2011, 17:36
  3. Moving a QButton around the form using the mouse
    By squidge in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2009, 20:19
  4. Change color of QButton does not work with GTK
    By Teuniz in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2009, 09:12
  5. Subclassing qbutton class
    By jingbo in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2006, 21:15

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.