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.
Related
I'm trying to connect signals from a child window (QDialog named VolumePage) to its parent (QMainWindow named MockUI). I'm running into a:
invalid use of incomplete type 'class Ui::VolumePage'
error when I attempt to make the connection.
I'm trying to connect inside the MockUI.cpp when I make the volume page. (Happens on a button press)
void MockUI::on_pushButton_3_clicked()
{
//Non-Modal Approach
volPage = new VolumePage(this);
volPage->show();
connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
}
I've made Ui::VolumePage *ui; public.
Here's the entire error:
error: invalid use of incomplete type 'class Ui::VolumePage'
connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
Can anyone help me understand what the problem is, or another clean way to do what I'm trying to do?
Edit: (Additional Source)
VolumePage.h:
#ifndef VOLUMEPAGE_H
#define VOLUMEPAGE_H
#include <QDialog>
namespace Ui {
class VolumePage;
}
class VolumePage : public QDialog
{
Q_OBJECT
public:
explicit VolumePage(QWidget *parent = 0);
Ui::VolumePage *ui;
~VolumePage();
private slots:
void on_verticalSlider_valueChanged(int value);
private:
};
#endif // VOLUMEPAGE_H
mockUI.cpp:
#include "mockUI.h"
#include "ui_mock_ics.h"
#include <QLCDNumber>
#include "volumepage.h"
MockUI::MockUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MockUI)
{
ui->setupUi(this);
}
MockUI::~MockUI()
{
delete ui;
}
void MockUI::on_pushButton_3_clicked()
{
//Modal Approach
//VolumePage volPage;
//volPage.setModal(true);
//volPage.exec();
//Non-Modal Approach
volPage = new VolumePage(this);
volPage->show();
connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
}
volumepage.cpp:
#include "volumepage.h"
#include "mockUI.h"
#include "ui_volumepage.h"
VolumePage::VolumePage(QWidget *parent) :
QDialog(parent),
ui(new Ui::VolumePage)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
//Mock_ICS* ics = (Mock_ICS*)parent;
//connect(ui->verticalSlider,SIGNAL(valueChanged(int)),ics->ui->progressBar,SLOT(setValue(int)));
//connect(ui->verticalSlider,SIGNAL(valueChanged(int)),this->parent()->progressBar,SLOT(setValue(int)));
}
VolumePage::~VolumePage()
{
delete ui;
}
Ui::VolumePage *ui should remain private. Instead create a signal on the volPage, something like verticalSliderValueChanged(int value). See below...
mockui.h
#ifndef MOCKUI_H
#define MOCKUI_H
#include <QMainWindow>
#include "volumepage.h"
namespace Ui {
class MockUI;
}
class MockUI : public QMainWindow
{
Q_OBJECT
public:
explicit MockUI(QWidget *parent = 0);
~MockUI();
VolumePage* volPage;
private slots:
void on_pushButton_3_clicked();
private:
Ui::MockUI *ui;
};
#endif // MOCKUI_H
mockui.cpp
#include "mockui.h"
#include "ui_mockui.h"
#include "volumepage.h"
MockUI::MockUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MockUI)
{
ui->setupUi(this);
}
MockUI::~MockUI()
{
delete ui;
}
void MockUI::on_pushButton_3_clicked()
{
//Non-Modal Approach
volPage = new VolumePage(this);
volPage->show();
connect(volPage,SIGNAL(verticalSliderValueChanged(int)),ui->progressBar,SLOT(setValue(int)));
}
volumepage.h
#ifndef VOLUMEPAGE_H
#define VOLUMEPAGE_H
#include <QDialog>
namespace Ui {
class VolumePage;
}
class VolumePage : public QDialog
{
Q_OBJECT
public:
explicit VolumePage(QWidget *parent = 0);
~VolumePage();
signals:
void verticalSliderValueChanged(int value);
private slots:
void on_verticalSlider_valueChanged(int value);
private:
Ui::VolumePage *ui;
};
#endif // VOLUMEPAGE_H
volumepage.cpp
#include "volumepage.h"
#include "ui_volumepage.h"
VolumePage::VolumePage(QWidget *parent) :
QDialog(parent),
ui(new Ui::VolumePage)
{
ui->setupUi(this);
}
VolumePage::~VolumePage()
{
delete ui;
}
void VolumePage::on_verticalSlider_valueChanged(int value)
{
emit verticalSliderValueChanged(value);
}
This is an example of the pImpl idiom in C++. Class VolumePage is the one you should be using - this is the "public" interface of what Qt Creator has generated. Ui::VolumePage contains the complicated details you should not worry about.
So, in your code you should be using VolumePage, not Ui::VolumePage. You shouldn't have make Ui::VolumePage public.
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 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);
}
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()));