I am having problem with "Signal and Slot" understanding. Here below is my task description.
Select an item from Combo Box
Display that selected item, by a Label in the mainwindow.
It is very simple thing but I am not able to solve it.
Here below is my code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void activated(QString);
private slots:
void setLabelValue(const QString &text);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->comboBox, &QComboBox::activated(QString),
this, &MainWindow::setLabelValue(QString));
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::setLabelValue(const QString &text)
{
//text = ui->comboBox->currentText();
text = static_cast<void (QComboBox::*)(QString)>(ui->comboBox->currentText());
ui->label->setText(text);
}
I hope you will forgive this novice's coding and understanding style.
The problem is you need to use the QLabel accessor function to change the text value. text() is a 'getter', not a 'setter'.
change MainWindow::setLabelValue() to
MainWindow::setLabelValue()
{
ui->label->setText(ui->comboBox->currentText());
}
Also, your connection isn't quite right. setLabelValue is a function of MainWindow, not the label, so change the connection to:
void (QComboBox::* activatedOverloadPtr)(const QString&) = &QComboBox::activated;
connect(ui->comboBox,activatedOverloadPointer, this, &MainWindow::setLabelValue);
Activated overload pointer is used so that the compiler can resolve which specific activated connection you are looking for.
The complete code would look something like:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
//signals: // this is defined by QComboBox, not you (in this case)
// void activated(QString);
private slots:
void setLabelValue(const QString &text);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
void (QComboBox::* activatedOverloadPtr)(const QString&) = &QComboBox::activated;
connect(ui->comboBox,activatedOverloadPointer, this, &MainWindow::setLabelValue);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::setLabelValue()
{
ui->label->setText(ui->comboBox->currentText());
}
Thanks all for your support. Here below the code, that has worked.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void setLabelValue(const QString &text);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->comboBox, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::activated), this,
&MainWindow::setLabelValue);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setLabelValue(const QString &text)
{
ui->label->setText(text);
}
Related
I overload dragEnterEvent() and dropEvent() in my MainWindow class, and call setAcceptDrops() in the constructor. While the running, I drag a .txt file in to the texteditor, but it's not showing the content of that .txt. Instead, it's show the path of that .txt. Please somebody help where I did wrong. Thanks.
//Header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Blockquote
//Source
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDragEnterEvent>
#include <QUrl>
#include <QFile>
#include <QTextStream>
#include <QMimeData>
#include <QList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event){
if(event->mimeData()->hasUrls())
event->acceptProposedAction();
else event->ignore();
}
void MainWindow::dropEvent(QDropEvent *event){
const QMimeData *mimeData = event->mimeData();
if(mimeData->hasUrls()){
QList<QUrl> urlList = mimeData->urls();
QString fileName = urlList.at(0).toLocalFile();
if(! fileName.isEmpty()){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly))return;
QTextStream in(&file);
ui->textEdit->setText(in.readAll());
}
}
}
You see this behavior because dropping is enabled on QTextEdit and the event is consumed there. (By default TextEdit drop copies filename into text area.)
In your constructor disable dropping on TextEdit by using
ui->textEdit->setAcceptDrops(false)
and then the event will be handled by the dropEvent method in MainWindow
I'm trying to connect a button to a function and after countless amounts of searches I couldn't find anything.
I could use a on_button_clicked(goto slot) function but I plan on re-assigning the button to run more code, so I decided to use the connect() class but when I run the application nothing happens when I type into the text box and click the game_user_input_submit button I have created, and im not sure if it is even calling the function.
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QtCore>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
user_name_set = false;
ui->game_chat_box->append("hi"); // a text edit box i created
if(user_name_set == false)
{
connect(ui->game_user_input_submit, SIGNAL(clicked()), SLOT(setUsername()));
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setUsername()
{
username = ui->game_user_input->text(); // get text from line edit i created
user_name_set = true;
ui->game_chat_box->append("BOT: Welcome " + username);
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
void setUsername();
~MainWindow();
private slots:
//void on_game_user_input_submit_clicked();
private:
bool user_name_set = false;
QString username;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
oh and I'm kinda new to programming c++ in general so if anybody does give me some usefull information I might need it to explain it to me, sorry
connect should be of the form:
connect(object_pointer, signal_name, slot_name)
In your case, you are setting the slot name to setUsername() which is not a slot - it is just a public function.
Your mainwindow.h should be:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void setUsername();
private:
bool user_name_set = false;
QString username;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
I am using Qt4 and Qt Creator. I cannot write a custom slot to a progress bar in the UI. How to write a custom slot for a specific widget in the ui file ? In my scenario, signal is not from the ui element.
the below code produces an error while running:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QFile>
#include<QFileInfo>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_btcopy_clicked();
void on_btquit_clicked();
void ChangeValue(qint64 val);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
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_btcopy_clicked()
{
QFileInfo *qfi=new QFileInfo("C:\\Users\\kiran\\Desktop\\test\\a.iso");
qDebug("%d" ,qfi->size());
QFile *qf=new QFile();
QFile fromFile("C:\\Users\\kiran\\Desktop\\test\\a.iso");
QFile toFile("C:\\Users\\kiran\\Desktop\\test\\b.iso");
ui->pbar->setMaximum(fromFile.size());
fromFile.copy(toFile.fileName());
connect(&toFile, SIGNAL(bytesWritten(qint64)), ui->pbar, SLOT(CangeValue(qint64)));
qDebug("completed");
}
void MainWindow::on_btquit_clicked()
{
exit(0);
}
void MainWindow::CangeValue(qint64 val)
{
ui->pbar->setValue(val);
}
Error Message
Object::connect: No such slot ProgressBar::CangeValue(qint64)in..\untitled\mainwindow.cpp:26
Object::connect: (receiver name: 'pbar')
CangeValue is a slot in your MainWindow (for the record: it should be called ChangeValue).
Therefore the third parameter in your connect(..) statement must be your main window, not the progress bar. Replace ui->pbar by this in your connect statement.
First of all, i posted the question after a lot of searching, reading and debugging ... but can't solve the problem.
I have two class MainWindow and Controller, both of them need to include the other. I tried to solve the problem of recursive include using forwarding, but it didn't work.
I think i have some problem with implementing forward with namespaces.
The code for both classes: (i tried to shorten the code as much as possible)
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include "controller.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void addRadioButtons();
void changeViewQML(QString);
void changeViewXML(QString);
private:
Ui::MainWindow *ui;
controller controllerObj;
};
#endif // MAINWINDOW_H
controller.h
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QtCore>
#include "mainwindow.h"
class controller : public QThread
{
public:
controller();
void response_handler(QString);
private:
MainWindow *viewObj;
protected:
void run();
};
#endif // CONTROLLER_H
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::changeViewQML(QString s)
{
qDebug(s.toAscii());
}
controller.cpp
#include "controller.h"
controller::controller()
{
}
void controller::response_handler(QString responseFilePath)
{
viewObj->changeViewQML(responseFilePath);
}
and i keep having this ISO error.
sorry for the long code, and if any part is not clear enough
just forward declare the MainWindow. Replace
#include "mainwindow.h" in the controller.h
with
namespace Ui {
class MainWindow;
}
also refer to it as Ui::MainWindow *viewObj; or write using namespace Ui in your controller.h
EDIT
this compiles fine:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include "controller.h"
namespace Ui{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void addRadioButtons();
void changeViewQML(QString);
void changeViewXML(QString);
private:
Ui::MainWindow *ui;
controller controllerObj;
};
#endif // MAINWINDOW_H
controller.h
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include <QtCore>
//#include "mainwindow.h"
class MainWindow;
class controller : public QThread
{
public:
controller();
void response_handler(QString);
private:
MainWindow *viewObj;
protected:
void run();
};
#endif // CONTROLLER_H
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::changeViewQML(QString s)
{
qDebug(s.toAscii());
}
controller.cpp
#include "controller.h"
#include "mainwindow.h"
controller::controller()
{
}
void controller::run()
{
}
void controller::response_handler(QString responseFilePath)
{
viewObj->changeViewQML(responseFilePath);
}
I'm trying to connect a signal from a QProcess inside my mainwindow() object to another QObject based class inside my mainwindow() object but I get this error:
QObject::connect: Cannot connect (null)::readyReadStandardOutput () to (null)::logReady()
Heres the code, its not complete by any means but I don't know why it doesn't work.
exeProcess.h
#ifndef EXEPROCESS_H
#define EXEPROCESS_H
#include <QObject>
class exeProcess : public QObject
{
Q_OBJECT
public:
explicit exeProcess(QObject *parent = 0);
signals:
void outLog(QString outLogVar); //will eventually connect to QTextEdit
public slots:
void logReady();
};
#endif // EXEPROCESS_H
exeProcess.cpp
#include "exeprocess.h"
exeProcess::exeProcess(QObject *parent) :
QObject(parent)
{
}
void exeProcess::logReady(){
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
#include "exeprocess.h"
/*main window ---------------------------------------*/
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QProcess *proc;
exeProcess *procLog;
public slots:
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(proc, SIGNAL(readyReadStandardOutput ()), procLog, SLOT(logReady()));
}
MainWindow::~MainWindow()
{
delete ui;
}
Thanks!.
You need to create the proc and procLog objects.
You've only got pointers as class members, so you'll have to initialize those (with new). connect only works on live objects.
proc is a pointer, but it does not point to anything. You have to instantiate a qprocess before you connect it!
proc = new QProcess();
connect(proc, SIGNAL(readyReadStandardOutput ()), procLog, SLOT(logReady()));