Results 1 to 4 of 4

Thread: Add code to form

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Aug 2017
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Add code to form

    Thanks for your response,

    i am very new to qt,
    i just need one basic example. actually my requirement is to develop one speedometer kind of application.

    i took a dialog widget at the time of creating a project.

    following are my files.
    main.cpp
    dialog.cpp
    dialog.h
    dialog.ui

    in dialog.ui, i added Push button and and line edit by drag dropping.
    In the same ui i need one circle with a marker, if i enter any value in lineedit and press the pushbutton, the marker position has to change according to the value.

    i implemented a small example as follows

    dialog.h
    **********
    // dialog.h

    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QPainter>
    6.  
    7. namespace Ui {
    8. class Dialog;
    9. }
    10.  
    11. class Dialog : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Dialog(QWidget *parent = 0);
    17. ~Dialog();
    18.  
    19. private:
    20. Ui::Dialog *ui;
    21.  
    22. protected:
    23. void paintEvent(QPaintEvent *e);
    24. };
    25.  
    26. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    ********
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Dialog)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Dialog::~Dialog()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Dialog:: paintEvent(QPaintEvent *e)
    17. {
    18. {
    19. QPainter painter(this);
    20.  
    21. QPen pen1(Qt::black);
    22. pen1.setWidth(5);
    23.  
    24. QPen pen2(Qt::red);
    25. pen2.setWidth(5);
    26.  
    27. QPen pen3(Qt::green);
    28. pen3.setWidth(5);
    29.  
    30. QPen pen4(Qt::blue);
    31. pen4.setWidth(5);
    32.  
    33. int x0 = 100;
    34. int y0 = 100;
    35. int width = 200;
    36. int height = 200;
    37. QRect rect(x0, y0, width, height);
    38.  
    39. // original rectangle
    40. painter.setPen(pen1);
    41. painter.drawRect(rect);
    42.  
    43.  
    44. // rotation
    45. // rotate around (0,0) which is top-left
    46. // rect center (xc,yc)
    47. int xc = x0 + width/2;
    48. int yc = y0 + height/2;
    49.  
    50. // order of transformation
    51. // (1) translate to top-left
    52. // (2) rotate
    53. // (3) move back to (xc,yc)
    54. //
    55. painter.translate(xc, yc); //(3)
    56. painter.rotate(45); //(2)
    57. painter.translate(-xc, -yc); //(1)
    58.  
    59. painter.setPen(pen2);
    60. painter.drawRect(rect);
    61.  
    62. // scale -> 1/2
    63. painter.translate(xc, yc);
    64. painter.scale(0.5,0.5);
    65. painter.translate(-xc, -yc);
    66.  
    67. painter.setPen(pen3);
    68. painter.drawRect(rect);
    69.  
    70. // Back to the initial state
    71. // shear
    72. painter.resetTransform();
    73. painter.translate(xc, yc);
    74. painter.shear(0.5,0.5);
    75. painter.translate(-xc, -yc);
    76.  
    77. painter.setPen(pen4);
    78. painter.drawRect(rect);
    79. }.
    To copy to clipboard, switch view to plain text mode 

    in the dialog.ui i havent done anything, but when i running the application empty dialog is opening

    i tried the below code
    http://www.bogotobogo.com/Qt/Qt5_QPa...sformation.php
    Last edited by high_flyer; 10th August 2017 at 14:48.

Similar Threads

  1. How to generate Bar Code of a Qt form ?
    By zed220 in forum Newbie
    Replies: 1
    Last Post: 7th September 2016, 15:10
  2. Replies: 3
    Last Post: 18th July 2013, 04:12
  3. Replies: 7
    Last Post: 23rd May 2012, 12:00
  4. How to hide from taskbar a form showed form mainwindow
    By tonnot in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2011, 14:36
  5. Replies: 5
    Last Post: 12th March 2010, 21:43

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
  •  
Qt is a trademark of The Qt Company.