Solved GUI for placing shapes in GraphicsView
-
@mrjj
In this case, I would like to create a new shape. When the user input the new value for x and y, it creates a new shape on that position.
Here's the GUI I modifyI added a button for creating a new shape every time I input a new value.
-
@Macive-Xiong
ok. super.
so when button calls the slot, you
simply read the values, and insert the new item.
This way you know when to create. -
OK, I put everything in the click button
void MainWindow::on_creat_btn_clicked() { QBrush redBrush(Qt::red); QBrush blueBrush(Qt::blue); QPen blackpen(Qt::black); blackpen.setWidth(2); ellipse = scene->addEllipse(x, y, 10, 10,blackpen,redBrush); }
Some suggestion for declaring the x and y ?? Thank you :)
-
hi
the spinedit has a value functionso
int x= ui->spin1->value();
int y= ui->spin2->value(); -
OMG!! It works!!!! Thank you soooo much. And sorry for those dumb questions...I am a noob on Qt and trying to create multiple projects to practice. Next step I would like to add the different colors while create different shapes and area.
Thank you again:)
-
@Macive-Xiong
No worries.
Beginner questions are very allowed/welcome here :)To select color, you can use
http://doc.qt.io/qt-5/qcolordialog.html#detailsits not much code to allow selection of color
QColor color = QColorDialog::getColor(Qt::yellow, this );
if( color.isValid() )
{
qDebug() << "Color Choosen : " << color.name();
}for type of shape, you could use a QCombobox
http://doc.qt.io/qt-4.8/qcombobox.html
so it has list of types. -
Thanks for your information. It's really helpful. And I must say, your kindness and patience are really important for the novice like me. Thanks again:)
-
@Macive-Xiong
You are most welcome.
Are you new to c++ too? -
Yep I kinda a new to C++. My projects are making connection with Qt and Arduino. Build a GUI on Qt and control Arduino. In this case, I would like to input position values on Qt and send these values to Arduino and do some math. Then send back to Qt and place the shape.
-
@Macive-Xiong
ok. just asking as if really new to C++ , some dont really realize that
adding variables to mainwindow.h makes it accessible to all methods.class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = 0);
~MainWindow();private slots:
void on_pushButton_released();void on_pushButton_3_released();
private:
Ui::MainWindow* ui;
<<<< good spot for variables.
};Also, if you are going to use serial comm with Arduino
then this sample is super to start with
http://doc.qt.io/qt-5/qtserialport-terminal-example.html -
OK, thanks for the information.
These are really helpful:)
Will ask more questions if I get more problem. -
Hi, now I am facing a problem.
When I input the value, say x = 500, y = 0. The ellipse shows at the center of my GraphicsView. My GraphicsView's boundary is 512*512.My code:
void MainWindow::on_creat_btn_clicked() { QBrush redBrush(Qt::red); QBrush blueBrush(Qt::blue); QPen blackpen(Qt::black); blackpen.setWidth(1); int x = ui->spin1->value(); int y = ui->spin2->value(); ellipse = scene->addEllipse(x, y, 5, 5,blackpen,redBrush); }
Any suggestion?? Thank you so much.
-
well you should read
http://doc.qt.io/qt-5/graphicsview.html
section
Scene Coordinatesyou can also define your own
scene->setSceneRect(-180, -90, 360, 180);Short story.
0,0, might not be where u think it is :)