[SOLVED] signal and slot between two classes
-
hello, i have declared one more class , if i use connect function i'm getting into on_item1_changed() loop but if i include table in that loop it shows a error like
@error: 'table4' was not declared in this scope
table4->clear();
@
please can anyone tel me what change i should do .@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QListWidget>
#include <QMouseEvent>
#include <QMimeData>
#include <QDrag>
#include <QDebug>
#include <QTabWidget>
#include <QTableWidget>
#include <QMimeType>
#include <QTcpSocket>
#include <QMessageBox>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
QTcpSocket *sendsocket;~MainWindow();
private:
Ui::MainWindow *ui;
public slots:
void on_item1_changed();};
class ProjectListWidget :public QTableWidget
{
Q_OBJECTpublic:
ProjectListWidget(QWidget *parent = 0);protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);private:
void performDrag();
QPoint startPos;};
#endif // MAINWINDOW_H
@@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);ProjectListWidget *table=new ProjectListWidget(ui->widget);
connect(table,signal(itemchanged()),this,slot(on_item1_changed()));
}ProjectListWidget::ProjectListWidget(QWidget *parent)
: QTableWidget(parent)
{
setAcceptDrops(true);}
void mainwindow :: on_item1_changed()
{
qdebug()<<"getting this loop";
table->clear; //getting error if i include this line but i want to include this what shall i do //
}
MainWindow::~MainWindow()
{
delete ui;}
@
-
Your code declares table inside the scope of the constructor. If you don't make it a member of the class, of course you can't access to it from anywhere else in the code, which is why the message you get says:"error: 'table4' was not declared in this scope"
-
The message from the compiler is simple English:
Sorry, the variables you want to alter is not within my reach! No idea where it is.
Like what Frank says, make the variable you want to alter a member variable and things should work. -
If you want "table" to be available in the scope of the entire class and not just in the scope of the constructor, you should put
@ProjectListWidget *table;@
in the header file.You may need to pick up a C++ book to help you with actual development.
-
You use ProjectListWidget in the MainWindow header but before you actually declared it - so obviously the compiler doesn't know what that class is.
Either put the ProjectListWidget in its own .h + .cpp files and #include them from your main window code OR do a forward declaration - as explained here:
http://stackoverflow.com/questions/2133250/does-not-name-a-type-error-in-c -
Isn't this supposed to be
@table->clear();@
?I'm sorry but I won't be able to remotely assist you through every compile step of code. Maybe you should start out by working with some basic Qt sample.
-
sorry ,here is my code i'm getting error if i try to discard so please do help.
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QListWidget>
#include <QMouseEvent>
#include <QMimeData>
#include <QDrag>
#include <QDebug>
#include <QTabWidget>
#include <QTableWidget>
#include <QMimeType>
#include <QTcpSocket>
#include <QMessageBox>namespace Ui {
class MainWindow;
}
class ProjectListWidget :public QTableWidget
{
Q_OBJECTpublic:
ProjectListWidget(QWidget *parent = 0);protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);private:
void performDrag();
QPoint startPos;};
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
ProjectListWidget *table4,*table5;
~MainWindow();private:
Ui::MainWindow *ui;
public slots:
void on_item1_changed();};
#endif // MAINWINDOW_H
@ -
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);ProjectListWidget *table=new ProjectListWidget(ui->widget);
table->setGeometry(0,0,750,580);
table->setColumnCount(1);
table->setRowCount(1);
table->horizontalHeader()->setDefaultSectionSize(750);
table->verticalHeader()->setDefaultSectionSize(580);
table->verticalHeader()->hide();
table->horizontalHeader()->hide();
table->setAcceptDrops(true);
setAcceptDrops(true);
table->setItem(0,0,new QTableWidgetItem("ITEM 1"));ProjectListWidget *table2=new ProjectListWidget(ui->widget_3);
table2->setGeometry(0,0,750,580);
table2->setColumnCount(1);
table2->setRowCount(1);
table2->horizontalHeader()->setDefaultSectionSize(750);
table2->verticalHeader()->setDefaultSectionSize(580);
table2->verticalHeader()->hide();
table2->horizontalHeader()->hide();
table2->setAcceptDrops(true);
setAcceptDrops(true);
table2->setItem(0,0,new QTableWidgetItem("ITEM 2"));ProjectListWidget *table3=new ProjectListWidget(ui->widget_4);
table3->setGeometry(0,0,750,580);
table3->setColumnCount(1);
table3->setRowCount(1);
table3->horizontalHeader()->setDefaultSectionSize(750);
table3->verticalHeader()->setDefaultSectionSize(580);
table3->verticalHeader()->hide();
table3->horizontalHeader()->hide();
table3->setAcceptDrops(true);
setAcceptDrops(true);
table3->setItem(0,0,new QTableWidgetItem("ITEM 3"));ProjectListWidget table4=new ProjectListWidget(ui->widget_2);
//table2->setGeometry(1000,40,270,161);
table4->setColumnCount(1);
table4->setRowCount(1);
table4->verticalHeader()->setDefaultSectionSize(200);
table4->horizontalHeader()->setDefaultSectionSize(350);
table4->horizontalHeader()->hide();
table4->verticalHeader()->hide();
table4->setAcceptDrops(true);
setAcceptDrops(true);
connect(table4,SIGNAL(itemChanged(QTableWidgetItem)),this,SLOT(on_item1_changed()));ProjectListWidget *table5=new ProjectListWidget(ui->widget_5);
//table2->setGeometry(1000,40,270,161);
table5->setColumnCount(1);
table5->setRowCount(1);
table5->verticalHeader()->setDefaultSectionSize(200);
table5->horizontalHeader()->setDefaultSectionSize(350);
table5->horizontalHeader()->hide();
table5->verticalHeader()->hide();
table5->setAcceptDrops(true);
setAcceptDrops(true);
ProjectListWidget *table6=new ProjectListWidget(ui->widget_6);
//table2->setGeometry(1000,40,270,161);
table6->setColumnCount(1);
table6->setRowCount(1);
table6->verticalHeader()->setDefaultSectionSize(200);
table6->horizontalHeader()->setDefaultSectionSize(350);
table6->horizontalHeader()->hide();
table6->verticalHeader()->hide();
table6->setAcceptDrops(true);
setAcceptDrops(true);
}ProjectListWidget::ProjectListWidget(QWidget *parent)
: QTableWidget(parent)
{
setAcceptDrops(true);
}
void ProjectListWidget::mousePressEvent(QMouseEvent *event)
{qDebug()<<"mouse press"; QWidget::mousePressEvent(event); if(event->button() == Qt::LeftButton) { }
}
void ProjectListWidget::mouseMoveEvent(QMouseEvent *event)
{
qDebug()<<"mouse move event";
if (event->buttons() & Qt::LeftButton) {
int distance = (event->pos() - startPos).manhattanLength();
if (distance >= QApplication::startDragDistance())
performDrag();
}
QTableWidget::mouseMoveEvent(event);
}
void ProjectListWidget::performDrag()
{
qDebug()<<"perform drag";
QTableWidgetItem *item = currentItem();
if (item) {
QMimeData *mimeData = new QMimeData;
mimeData->setText(item->text());QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); QString fileName = "C:/Users/teclever/Desktop/qt-logo.jpg"; QPixmap pic (fileName); drag->setPixmap(pic); if (drag->exec(Qt::CopyAction) == Qt::CopyAction) { } }
}
void ProjectListWidget::dragEnterEvent(QDragEnterEvent *event)
{
qDebug()<<"drag enter";
ProjectListWidget *source =
qobject_cast<ProjectListWidget *>(event->source());
if (source && source != this) {
event->setDropAction(Qt::CopyAction);
event->accept();
}
}
void ProjectListWidget::dragMoveEvent(QDragMoveEvent *event)
{
qDebug()<<"drag move event";
ProjectListWidget *source =
qobject_cast<ProjectListWidget *>(event->source());
if (source && source != this) {
event->setDropAction(Qt::MoveAction);
setAcceptDrops(true);
event->acceptProposedAction();
}
}
@ continue -
@#if 1
void ProjectListWidget::dropEvent(QDropEvent *event)
{qDebug()<<"drop event"; ProjectListWidget *source = qobject_cast<ProjectListWidget *>(event->source()); if (source && source != this) { QString str=event->mimeData()->text(); qDebug()<<"str=="<<str; QTableWidgetItem *it=new QTableWidgetItem; setItem(0,0,new QTableWidgetItem(str)); // insertRow(rowCount()); event->setDropAction(Qt::MoveAction); event->accept(); event->acceptProposedAction(); QTableWidget::dropEvent(event);
}
QTableWidget::dropEvent(event);
}
#endif
void MainWindow::on_item1_changed()
{
qDebug()<<"changed";QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.setInformativeText("Do you want to save your changes?"); msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard ); msgBox.setDefaultButton(QMessageBox::Save); int ret = msgBox.exec(); switch (ret) { case QMessageBox::Save: qDebug()<<"Save was clicked"; break; case QMessageBox::Discard: qDebug()<<" Don't Save was clicked"; table4->clear(); break; case QMessageBox::Cancel: // Cancel was clicked break; default: // should never be reached break; }
}
MainWindow::~MainWindow()
{
delete ui;}
@ -
What is the exact error you get and when do you get it (when you compile? when you run?)
-
thanks for giving your time. In on_item1_changed() loop i have used qmessage box so that when item changes it ask for save and discard ,i want to clear the table if i press discard so i included
@table4->clear();@
but after running this code when i press discard i'm getting .exe not working. actually i cant able to perform any actions on table4. -
OK, good to know.
By the way, when changing the post topic to solved, it's best to add [SOLVED] in the beginning of the title, not at the end.