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
}
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.
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);
Pretty much this window opens up and asks for a bandname. I got it so the characters on the line edit widget get stored in a variable. Problem is I have another file called main window.cpp and I want that variable to to be stored on the list widget on that window. Now I know how to display things on the list widget but I can't figure out a way to get the text after the user finished typing. The bandname var in the main window.cpp file just takes an empty string and I know why but is there any way to trigger the get call after the user has finished typing. Do I have to restrict something in the class like the get function. I've experimented a lot and saw callbacks but I could just use signals and slots. Everything Ive tried just returns an empty string but I need the text after the user has finished typing what he wants. Here is the dialog window named add button
#include "addbutton.h"
#include "ui_addbutton.h"
AddButton::AddButton(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddButton)
{
ui->setupUi(this);
connect(ui->cancel,SIGNAL(released()),this,SLOT(close()));
//Get Text when user presses enter
connect(ui->lineEdit, SIGNAL(editingFinished()),this,SLOT(setBandName()));
}
void AddButton::setBandName(){
bandname = ui->lineEdit->text();
}
void AddButton::updateState(){
pbandname = bandname;
}
QString AddButton::getBandName(){
return bandname;
}
AddButton::~AddButton()
{
delete ui;
}
Here is the main window.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "addbutton.h"
#include "bandinfo.h"
#include "QDebug"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mediaplayer = new Player;
connect(ui->pushbutton_addBand,SIGNAL(pressed()),this,SLOT(addBand()));
}
void MainWindow::addBand(){
BandInfo band;
AddButton *addband_window = new AddButton;
QString bandname;
addband_window->show();
bandname = addband_window->pbandname;
qDebug() << bandname;
}
MainWindow::~MainWindow()
{
delete ui;
}
I'm not sure I understand your post. Are you saying you want the main window to be notified/updated when the user finishes editing in the "AddButton" class?
If I've got that right it seems pretty straightforward:
Add a signal to the AddButton class. Call it something like "bandNameChanged".
Make the signal pass a string as its argument
Emit the signal from within AddButton::setBandName and pass the string name.
Have the main window connect a slot to the "bandNameChanged" signal when it creates the AddButton.
In the slot, update your list widget
I have a MainWindow class and Another class. Another class has method createView that create new QGraphicsView. This method I call from MainWindow and I also want to layout this view on my form. It looks like:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)) {
...
AnotherClass object(this);
object.createView();
...
}
...
void AnotherClass::createView() {
QGraphicsView *gv= new QGraphicsView(mainWindow); // mainWindow - pointer to MainWindow object
gv->show();
}
But it doesn't work so good... actually it does't work at all. And yes, I save pointer on MainWindow object in my Another class as mainWindow, that I take from Another class constructor.
If use
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)) {
...
QGraphicsView *gv= new QGraphicsView(this);
gv->show();
...
}
It will work fine, but this solution doesn't satisfied me.
Problem was in creating variable on stack.