Results 1 to 4 of 4

Thread: Add code to form

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

    Default Add code to form

    how to add dynamic code to ui form

  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: Add code to form

    Maybe if you offered even less details we could help.
    Seriously, we are not telepaths.
    Elaborate on the problem, show the code you have, explain what you have tried write the post as if you actually want to receive an answer.
    ==========================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
    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 15:48.

  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: Add code to form

    I edited your post to put the code segments in [CODE] sections, please do so in the future, it makes reading the posts easier.

    The level of question based on how you explained what you have done shows basically no knowledge of Qt what so ever.
    Its hard to teach you through forum posts.
    The forum is to help you with specific questions or problems, we can't hold your hand and feed you the knowledge.
    You really should start with reading the documentation and going through various examples.
    Come back when you have specific questions about how to do specific stuff or problems.

    Never the less, the analog clock example should cover most of the things you will need in your case (as far as I can tell), it could be a good place to start:
    http://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html
    ==========================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. The following user says thank you to high_flyer for this useful post:

    bharathballa (11th August 2017)

Similar Threads

  1. How to generate Bar Code of a Qt form ?
    By zed220 in forum Newbie
    Replies: 1
    Last Post: 7th September 2016, 16:10
  2. Replies: 3
    Last Post: 18th July 2013, 05:12
  3. Replies: 7
    Last Post: 23rd May 2012, 13: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, 15:36
  5. Replies: 5
    Last Post: 12th March 2010, 22: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.