Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] signal and slot between two classes

    General and Desktop
    4
    18
    3239
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E
      ecmanja last edited by

      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_OBJECT

      public:
      explicit MainWindow(QWidget *parent = 0);
      QTcpSocket *sendsocket;

      ~MainWindow();
      

      private:
      Ui::MainWindow *ui;
      public slots:
      void on_item1_changed();

      };
      class ProjectListWidget :public QTableWidget
      {
      Q_OBJECT

      public:
      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;

      }

      @

      1 Reply Last reply Reply Quote 0
      • F
        frankiefrank last edited by

        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"

        "Roads? Where we're going, we don't need roads."

        1 Reply Last reply Reply Quote 0
        • Jeroentjehome
          Jeroentjehome last edited by

          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.

          Greetz, Jeroen

          1 Reply Last reply Reply Quote 0
          • E
            ecmanja last edited by

            sorry i'm not getting where to make member of a class can u please tell me where shall i include and what should i include

            1 Reply Last reply Reply Quote 0
            • F
              frankiefrank last edited by

              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.

              "Roads? Where we're going, we don't need roads."

              1 Reply Last reply Reply Quote 0
              • E
                ecmanja last edited by

                i included this in mainwindow class after compling i got error: 'ProjectListWidget' does not name a type
                ProjectListWidget *table4; what shall i do now

                1 Reply Last reply Reply Quote 0
                • F
                  frankiefrank last edited by

                  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

                  "Roads? Where we're going, we don't need roads."

                  1 Reply Last reply Reply Quote 0
                  • E
                    ecmanja last edited by

                    thanks for your link i solved that error but when i include
                    @table->clear@
                    im getting .exe stopped working :(
                    i can't able to perform any action to "table". not getting any error while compling.

                    1 Reply Last reply Reply Quote 0
                    • F
                      frankiefrank last edited by

                      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.

                      "Roads? Where we're going, we don't need roads."

                      1 Reply Last reply Reply Quote 0
                      • E
                        ecmanja last edited by

                        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_OBJECT

                        public:
                        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_OBJECT

                        public:
                        explicit MainWindow(QWidget *parent = 0);
                        ProjectListWidget *table4,*table5;
                        ~MainWindow();

                        private:
                        Ui::MainWindow *ui;
                        public slots:
                        void on_item1_changed();

                        };

                        #endif // MAINWINDOW_H
                        @

                        1 Reply Last reply Reply Quote 0
                        • E
                          ecmanja last edited by

                          @#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&#40;Qt::CopyAction&#41; == 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

                          1 Reply Last reply Reply Quote 0
                          • E
                            ecmanja last edited by

                            @#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&#40;&#41;;
                            switch (ret&#41; {
                              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;

                            }
                            @

                            1 Reply Last reply Reply Quote 0
                            • F
                              frankiefrank last edited by

                              What is the exact error you get and when do you get it (when you compile? when you run?)

                              "Roads? Where we're going, we don't need roads."

                              1 Reply Last reply Reply Quote 0
                              • E
                                ecmanja last edited by

                                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.

                                1 Reply Last reply Reply Quote 0
                                • E
                                  ecmanja last edited by

                                  thank you very much frankiefrank i solved the issue :)

                                  1 Reply Last reply Reply Quote 0
                                  • G
                                    goblin last edited by

                                    What was the issue?

                                    1 Reply Last reply Reply Quote 0
                                    • E
                                      ecmanja last edited by

                                      i have declared it twice so got that error i.e we must use only "table4" instead of "projectlistwidget *table4" in .cpp because it is already declared in .h

                                      1 Reply Last reply Reply Quote 0
                                      • F
                                        frankiefrank last edited by

                                        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.

                                        "Roads? Where we're going, we don't need roads."

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post