Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Japanese
  4. 他クラスからMainwindowクラスのメンバ変数を使う方法
Forum Updated to NodeBB v4.3 + New Features

他クラスからMainwindowクラスのメンバ変数を使う方法

Scheduled Pinned Locked Moved Solved Japanese
3 Posts 2 Posters 3.8k Views 1 Watching
  • 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.
  • taku-sT Offline
    taku-sT Offline
    taku-s
    wrote on last edited by taku-s
    #1

    またまた、お世話になります。
    フォームにラベルを貼り、QTimerの割り込みから、1秒毎にラベルのテキストを変更しようとしています。
    Mainwindowにまとめると、簡単にできるのですが、勉強のため、QTimerを別クラスに分けて、Mainwindowの
    uiを操作することを試してみました。
    一つの方法として、Mainwindowクラスを継承して見ましたが、コンパイラは通るのですが上手く動きません。
    アプリケーション出力欄には、
    QObject::connect: No such slot MainWindow::TimerSlot() in ../test1/timer.cpp:6
    QObject::connect: (receiver name: 'MainWindow')
    と、出ています。Timer::TimeSlot()のはずですが、
    TimerクラスにQ_OBJECTマクロが無いのが原因かと思うのですが
    解決方はあるでしょうか?
    長くなりますが、以下コードです

    [main.cpp]

    #include "mainwindow.h"
    #include "timer.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        Timer t;
        w.show();
        return a.exec();
    }
    

    [mainwindow.h]

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    public slots:
        void on_pushButton1_clicked();
        void on_pushButton2_clicked();
    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_pushButton1_clicked()
    {
        ui->label->setText("黒");
    }
    
    void MainWindow::on_pushButton2_clicked()
    {
        ui->label->setText("白");
    }
    

    [timer.h]

    #ifndef TIMER_H
    #define TIMER_H
    #include <QTimer>
    #include "mainwindow.h"
    
    namespace Ui {
    class Timer;
    }
    
    class MainWindow;
    
    class Timer : public MainWindow
    {
    private:
        bool tflag;
    public:
        Timer();
        ~Timer();
        QTimer* timer;
    public slots:
        void TimerSlot();
    };
    #endif // TIMER_H
    

    [timer.cpp]

    #include "timer.h"
    
    Timer::Timer()
    {
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(TimerSlot()));
        timer->start(1000);
        tflag = false;
    }
    
    Timer::~Timer(){
        delete timer;
    }
    
    void Timer::TimerSlot(){
        if(tflag){
            on_pushButton1_clicked();
        }
        else{
            on_pushButton2_clicked();
        }
        tflag = !tflag;
    }
    
    JKSHJ 1 Reply Last reply
    0
    • taku-sT taku-s

      またまた、お世話になります。
      フォームにラベルを貼り、QTimerの割り込みから、1秒毎にラベルのテキストを変更しようとしています。
      Mainwindowにまとめると、簡単にできるのですが、勉強のため、QTimerを別クラスに分けて、Mainwindowの
      uiを操作することを試してみました。
      一つの方法として、Mainwindowクラスを継承して見ましたが、コンパイラは通るのですが上手く動きません。
      アプリケーション出力欄には、
      QObject::connect: No such slot MainWindow::TimerSlot() in ../test1/timer.cpp:6
      QObject::connect: (receiver name: 'MainWindow')
      と、出ています。Timer::TimeSlot()のはずですが、
      TimerクラスにQ_OBJECTマクロが無いのが原因かと思うのですが
      解決方はあるでしょうか?
      長くなりますが、以下コードです

      [main.cpp]

      #include "mainwindow.h"
      #include "timer.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          Timer t;
          w.show();
          return a.exec();
      }
      

      [mainwindow.h]

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      #include <QMainWindow>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      public slots:
          void on_pushButton1_clicked();
          void on_pushButton2_clicked();
      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_pushButton1_clicked()
      {
          ui->label->setText("黒");
      }
      
      void MainWindow::on_pushButton2_clicked()
      {
          ui->label->setText("白");
      }
      

      [timer.h]

      #ifndef TIMER_H
      #define TIMER_H
      #include <QTimer>
      #include "mainwindow.h"
      
      namespace Ui {
      class Timer;
      }
      
      class MainWindow;
      
      class Timer : public MainWindow
      {
      private:
          bool tflag;
      public:
          Timer();
          ~Timer();
          QTimer* timer;
      public slots:
          void TimerSlot();
      };
      #endif // TIMER_H
      

      [timer.cpp]

      #include "timer.h"
      
      Timer::Timer()
      {
          timer = new QTimer(this);
          connect(timer, SIGNAL(timeout()), this, SLOT(TimerSlot()));
          timer->start(1000);
          tflag = false;
      }
      
      Timer::~Timer(){
          delete timer;
      }
      
      void Timer::TimerSlot(){
          if(tflag){
              on_pushButton1_clicked();
          }
          else{
              on_pushButton2_clicked();
          }
          tflag = !tflag;
      }
      
      JKSHJ Online
      JKSHJ Online
      JKSH
      Moderators
      wrote on last edited by JKSH
      #2

      @taku-s said in 他クラスからMainwindowクラスもメンバ変数を使う方法:

      TimerクラスにQ_OBJECTマクロが無いのが原因かと思うのですが

      正しいです。シグナルとスロットを使うにはQ_OBJECTマクロが必要です。

      解決方はあるでしょうか?

      Q_OBJECTマクロを追加してください。

      class Timer : public MainWindow
      {
          Q_OBJECT
      private:
          bool tflag;
          ...
      

      その後、qmakeを実行してプログラムをビルドしてください。

      しかし、実際には、タイマーはウィンドウではありません。 したがって、TimerクラスはMainWindowクラスを継承しないでください。代わりにQObjectクラスを継承するだけです。

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • taku-sT Offline
        taku-sT Offline
        taku-s
        wrote on last edited by
        #3

        JKSHさま
        ご回答、いただきありがとうございました。
        MainWindowクラスの継承でなく、インスタンスの参照渡しで、やってみます。

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved