[SOLVED] Simple SetText example
-
The
@
private slots:
@part belongs to the header, not the .cpp file. If you already have it in the header, then you probably have a typo (missing bracket, parenthesis, etc).
-
[quote author="panosk" date="1388347381"]The
@
private slots:
@part belongs to the header, not the .cpp file. If you already have it in the header, then you probably have a typo (missing bracket, parenthesis, etc). [/quote]
Same error when including it in the header file:
Header:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTableWidget>
#include "Equations.h"namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;
};private slots:
#endif // MAINWINDOW_H
@ -
It's out of the class definition and you forgot the actual slot function.
-
:) Try putting private slots inside the class definition. Like this:
@class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;private slots:
void mySlot() {
//If you like, do some checks for empty string, etc
ui->lineEdit_2->setText("Hello");
}}; @
EDIT: panosk, we are really syncronized... :) :)
-
This time I get the error
invalid use of incomplete type 'class Ui::MainWindow'
and
forward declaration of 'class Ui::MainWindow'on lines 9 and 26 respectively.
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTableWidget>
#include "Equations.h"namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;private slots:
void mySlot() {
//If you like, do some checks for empty string, etc
ui->lineEdit_2->setText("Hello");
}
};#endif // MAINWINDOW_H
@ -
Use only the definition in the header and put the implementation in your source file.
This goes in the header:
@
private slots:
void mySlot();
@And this goes to the .cpp file:
@
void MainWindow::mySlot() {
ui->lineEdit_2->setText("Hello");
}
@ -
Great, it works, thanks.
Question though. On line 16, why must I use "this" as the third connect parameter instead of "lineEdit_2"?
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableWidget>
#include "stdio.h"#include <Equations.h>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);connect (ui ->lineEdit, SIGNAL (editingFinished()), this, SLOT (mySlot()));
}
MainWindow::~MainWindow()
{delete ui;
}
void MainWindow::mySlot() {
ui->lineEdit_2->setText("Hello");
}
@ -
You don't need it in this case, it's just using another overload of the connect function. All version of the connect function where not present in QObject at the same time, so older user are generally using the form with 4 arguments
-
If I replace "this" with ui->linEdit_2, the slot doesnt trigger. How does one explain this?
-
The slot is a member of your MainWindow class and it's not a member of QLineEdit's class (lineEdit_2 is a QLineEdit).
The connect statement connects a signal from an object A to a slot of an object B. In your case, you connect the signal returnPressed() which indeed is a member of a QLineEdit object (your lineEdit) and the slot mySlot() which indeed is a member of your current (this) MainWindow object.