I have a Qt gui project and in the "mainwindow.cpp" file I have to define a function that I cannot declare under "mainwindow.h". But I want to call that function (func_sqrt) under MainWindow and show the result value of my func_sqrt in a label. For some reason I need to do that so. But I don't know how to connect that function to the gui objects. My code looks like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
QString input;
void func_sqrt(int x);
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
func_sqrt(2);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showtext(QString txt)
{
ui->lbl_value->setText(txt);
}
void func_sqrt(int x)
{
int y;
y = x*x;
}
I added this part to the func_sqrt function, but it doesn't work:
MainWindow *w = new MainWindow;
w->showtext(QString::number(y));
First of all, your func_sqrt currently is a no-op. Next, internally it calculates a square yet its name says sqrt (short for square root), you might want to check whether the name is consistent with semantics. This is for the side notes.
If you need this function available outside of mainwindow.cpp, you can declare it in a separate header and include it everywhere as needed. Alternatively, you can declare it in every file it is used in, the linker will later resolve the actual implementation. For instance, in a file subordinatewindow.cpp:
void func_sqrt(int);
// ...
int x{42};
func_sqrt(x);
Related
I am currently working in QT and recently I noticed something that really confused me.
As far as I can tell normally when we want to create a pointer we have to use the following syntax in C++:
int number = 10;
int* pNumber = &number;
(or something similar to that)
I wanted to create a pointer to a button which was created in QT design. It was for testing purposes only. (I am new to QT and c++ so I wanted to test things out)
But then I noticed something strange that I could not understand. for some reason when I created the pointer of type "OPushButton" with the name of "button" I did not have to use the "&" with the "(*ui).pushButton_5" syntax. (pushButton_5 is the name of my button in my ui)
The code works and the text "5" is added to my "lineEdit" in QT. How does this work? Am I missing something about pointers?
Here is my code:
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton* button = (*ui).pushButton_5;
ui->lineEdit->setText((*button).text());
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.h:
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
& is not the way to create pointers, it's the way to acquire a pointer to a specific thing that you have access to.
If somebody else tells you where a thing is, you don't need the help of & to find out.
void g(int* q)
{
int* p = q; // 'q' is the location of some unknown 'int', and so is `p`.
}
You need & if you have a thing and want to know where that thing is.
void f()
{
int x = 5;
int* p = &x; // The location of 'x'.
g(&x); // Pass the location of 'x' to 'g'.
}
Also, we usually write x->y rather than (*x).y.
This convention makes a lot of sense if you look at more than one level of indirection – compare x->y->z->w to (*(*(*x).y).z).w.
I'm a beginner in C++. I was making a test application in Qt and came across this problem: Where do I have to declare a variable in order to use it in a function like on_pushButton_clicked() ?
I tried making a main function and declaring the variable there and always got this error when changing the variable in another function:
error: reference to non-static member function must be called; did you mean to call it with no arguments?
Then I tried declaring the variable directly (not in any function) but that didn't work either.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
int main() {
int x = 0;
return 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
x++; //here's the error
ui->label->setText("number is:");
}
Is there a way to declare that variable (x) so that I can access it through on_pushButton_clicked()?
I managed to make it work: the problem was that I couldn't find, at first, the header file containing the class. That's where I should have declared my variable.
Here, I am just trying to obtain the changed int value from QSpinBox by using SIGNAL and SLOTS because I need to use this int variable in my another function. I mean if the user changes QSpinBox values, it changes the value of int variable.
I know this SIGNAL will be SIGNAL(valueChanged(int)), but I got confused about what the SLOT will be in this case. I really got stack on this point.
EDIT:
What I have tried is following. MainWindow.h is
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public slots:
void setFrame(int frame);
MainWindow.cpp is following. To check how it is working I tried to display the int in a QLabel.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(setFrame(int frame)));
}
void MainWindow::setFrame(int frame)
{
ui->label->setText(QString::number(frame));
}
Can anyone fix this regarding how to get the changed int value from QSpinBox?
Thanks in advance.
As the documentation states (in this document about QObject Class):
Note that the signal and slots parameters must not contain any variable names
So your mistake is specifying the variable name in the slot SLOT(setFrame(int frame)). As you must only use the variable type, it would be instead SLOT(setFrame(int)).
Anyway, if that's the only objective of the connect, I would rather do:
connect(ui->spinBox, SIGNAL(valueChanged(int)), ui->label, SLOT(setNum(int)));
That way, you don't even need the method setFrame in your MainWindow and labels already have a way to show numbers instead of strings.
Hope this helps!
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
audioRecorder = new QAudioRecorder;
QAudioEncoderSettings audioSettings;
audioSettings.setCodec("audio/amr");
audioSettings.setQuality(QMultimedia::HighQuality);
audioRecorder->setEncodingSettings(audioSettings);
audioRecorder->setOutputLocation(QUrl::fromLocalFile("test.amr"));
audioRecorder->record();
}
void MainWindow::on_pushButton_2_clicked()
{
//How to use audioRecorder variable???
}
I want to use the audioRecorder variable into the last method of my code. because when I declare audioRecorder = new QAudioRecorder;, the variable audio recorder is only accessible into the method on_pushButton_clicked(), so I want to make this variable usable into the method n_pushButton_2_clicked(). How to do it ?
Like some person said in the comments, you need to have your QAudioRecorder object declared as a member of your MainWindow class.
So basically you need something like this :
QAudioRecorder* audioRecorder;//This could also be a shared_ptr
Then, your audioRecorder object is created in your on_pushButton_clicked() function.
You can now use it in on_pushButton_2_clicked() function.
However, this is quite unsafe, for example, if you did not create the audioRecorder before calling on_pushButton_2_clicked(), audioRecorder is not going to point to a valid object and this will most certainly crash your project.
So before using it in on_pushButton_2_clicked(), verify if its valid:
if(audioRecorder)
{
// You can use audioRecorder safely here
}
I'm new to programming and using qt to create my own GUI. I'm trying to make a search bar one of my list view's but it keeps saying that there is no matching function to call... This may be a really stupid question. Here is my code.
void Widget::on_SearchBar_textChanged(const QString &arg1)
{
QString Input;
ui->Online->find(Input);
}
and the error
C:\Qt\Qt5.1.1\Tools\QtCreator\bin\CryptoCourier\widget.cpp:21: error: no matching function for call to 'QListWidget::find(QString&)'
ui->Online->find(Input);
here is the rest of my code as requested
Ok So here is the rest of my code. Not much but here.
#include "widget.h"
#include "ui_CryptoCC.h"
#include <QString>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_SearchBar_textChanged(const QString &arg1)
{
#include <string>
QString Input;
ui->Online->find(Input);
}
^
You have two major problems:
#include statements should go outside of functions, since they literally include an entire file exactly where you put them.
For QString, the file you want to include is probably called "QString".
Try something like this:
#include <QString>
/* the rest of your code, which you didn't include in your example */
void Widget::on_SearchBar_textChanged(const QString &arg1)
{
/* by the way, you're calling Online->find() with an empty string,
* did you mean to use `arg1` here? */
QString Input;
ui->Online->find(Input);
}
Beyond that, I'd need to know what ui and ui->Online are before I could give you advice about what functions you can call on them.