PDA

View Full Version : Add code to form



bharathballa
10th August 2017, 07:20
how to add dynamic code to ui form

high_flyer
10th August 2017, 11:54
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.

bharathballa
10th August 2017, 12:52
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


#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPainter>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();

private:
Ui::Dialog *ui;

protected:
void paintEvent(QPaintEvent *e);
};

#endif // DIALOG_H

dialog.cpp
********

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog:: paintEvent(QPaintEvent *e)
{
{
QPainter painter(this);

QPen pen1(Qt::black);
pen1.setWidth(5);

QPen pen2(Qt::red);
pen2.setWidth(5);

QPen pen3(Qt::green);
pen3.setWidth(5);

QPen pen4(Qt::blue);
pen4.setWidth(5);

int x0 = 100;
int y0 = 100;
int width = 200;
int height = 200;
QRect rect(x0, y0, width, height);

// original rectangle
painter.setPen(pen1);
painter.drawRect(rect);


// rotation
// rotate around (0,0) which is top-left
// rect center (xc,yc)
int xc = x0 + width/2;
int yc = y0 + height/2;

// order of transformation
// (1) translate to top-left
// (2) rotate
// (3) move back to (xc,yc)
//
painter.translate(xc, yc); //(3)
painter.rotate(45); //(2)
painter.translate(-xc, -yc); //(1)

painter.setPen(pen2);
painter.drawRect(rect);

// scale -> 1/2
painter.translate(xc, yc);
painter.scale(0.5,0.5);
painter.translate(-xc, -yc);

painter.setPen(pen3);
painter.drawRect(rect);

// Back to the initial state
// shear
painter.resetTransform();
painter.translate(xc, yc);
painter.shear(0.5,0.5);
painter.translate(-xc, -yc);

painter.setPen(pen4);
painter.drawRect(rect);
}.

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_QPainter_Transformation.php

high_flyer
10th August 2017, 15:54
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