How to create a button and place it where I want and then run a function when the button is clicked C++ QT
-
@RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:
qt c++
There is no such thing. Qt is simply a C++ framework.
Where did you write this code please? In constructor of your class.
You should use layouts to position widgets, see https://doc.qt.io/qt-6/layout.html
To trigger actions when a button is clicked use signals/slots: https://doc.qt.io/qt-6/signalsandslots.htmlThis makes no sense:
m_button->clicked();
clicked() is a signal and will be called by Qt as soon as user clickes then button. Connect a slot to this signal.
-
@jsulm
thank,
I wrote it in this function (if you know from application- example)void MainWindow::open() //! [7] //! [8] { QPushButton *m_button; m_button = new QPushButton("My Button", this); m_button->show(); m_button = new QPushButton("My Button", mainWind); m_button->setGeometry(150, 150,150,150); m_button->clicked(); if (maybeSave()) { QString fileName = QFileDialog::getOpenFileName(this); if (!fileName.isEmpty()) loadFile(fileName); QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; StartToSendCommand(fileName, textEdit);//Ruth } }
-
@RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:
I wrote it in this function (if you know from application- example)
From what example? Please provide a link.
But also follow what @jsulm is asking/saying. -
@RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:
what can I do instaed of?
First: explain CLEARLY what you want to do.
And please learn Qt basic stuff like signals/slots (read the links I posted).
"qt c++" - again, there is no such thing. Are you new to Qt, or also to C++?
If you need a button to pen a dialog then it should look like this:MainWindow::MainWindow(...) { m_button = new QPushButton("My Button", this); m_button->show(); connect(m_button, &QPushButton::clicked, this, &MainWindow::open); } void MainWindow::open() { if (maybeSave()) { QString fileName = QFileDialog::getOpenFileName(this); if (!fileName.isEmpty()) loadFile(fileName); QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; StartToSendCommand(fileName, textEdit);//Ruth }
-
@jsulm said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:
thank you!!I'll explain a little what I meant, I want to have the option of opening a file, and after opening the file a "start testing" button will appear, clicking on it will activate the function StartToSendCommand() which receives QString fileName, QPlainTextEdit *textEdit - which is a problem according to the option you gave me, it doesn't give in the function connect() to send parameters. Then I want the function to run on the file I opened... Is my question more understandable?
-
@RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:
connect()
Connect is a method in QObject - all subclasses including QPlainTextEdit have it.
Modified version according your description (I don't understand why you're opening file without doing anything with its content, I also don't know what this textEdit is about and why it needs to be parameter, your code is really strange - I suggest you start from scratch step by step):void MainWindow::open() { if (maybeSave()) { QString fileName = QFileDialog::getOpenFileName(this); QPushButton *button = new QPushButton("Start", this); connect(button, &QPushButton::clicked, this, [=]() { sendCommand(fileName, textEdit); }); } void MainWindow::sendCommand(QString fileName, QTextEdit textEdit) { if (!fileName.isEmpty()) loadFile(fileName); QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; StartToSendCommand(fileName, textEdit);//Ruth }
-
I open a file and use its contents!
Basically, I open a txt file which is a file consisting of commands and I want that after the file is selected, it will be possible to click on "start testing" - then it will start running line by line from the file and the commands will be sent, so I actually need this whole process.
After what I've explained here, is the code understandable, or does it still not look professional (I'd appreciate comments, I'm new...)and- @jsulm- I dont have link its in my software
-
@RuWex said in How to create a button and place it where I want and then run a function when the button is clicked C++ QT:
I open a file and use its contents!
Well, not here:
void MainWindow::open() { QPushButton *m_button; m_button = new QPushButton("My Button", this); m_button->show(); m_button = new QPushButton("My Button", mainWind); m_button->setGeometry(150, 150,150,150); m_button->clicked(); if (maybeSave()) { QString fileName = QFileDialog::getOpenFileName(this); if (!fileName.isEmpty()) loadFile(fileName); QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) // You open the file here but DO NOT USE IT return; StartToSendCommand(fileName, textEdit);//Ruth }
What second question do you mean?
-
I have this code:
QPushButton *m_button= new QPushButton ("Start Testing", this) ;
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(m_button,150,150, 100,140);
m_button->show();how can I place the button, for example in the middle of the window? I use it:
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(m_button,150,150, 100,140);
but its not help... and do nothing can anyone help me?thanks!!!
-
@RuWex I answered this question in my first post in this thread: "You should use layouts to position widgets, see https://doc.qt.io/qt-6/layout.html"