Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How do I connect a signal and slot to call function in MainWindow?
Forum Updated to NodeBB v4.3 + New Features

How do I connect a signal and slot to call function in MainWindow?

Scheduled Pinned Locked Moved Solved Installation and Deployment
slot signal qt5
4 Posts 2 Posters 751 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.
  • D Offline
    D Offline
    desun_evan
    wrote on last edited by desun_evan
    #1

    Hi, I am pretty new to c++ and Qt5. I am trying to connect a push button in a second window to a function in MainWindow to update a QTextBrowser (in MainWindow)

    I created a QWidget and connected the QPushButton to a slot in MainWindow.h, this opens a second window.

    void MainWindow::on_pushButton_clicked()
    {
        QWidget *r = new window2;
    
    r->show();
    
    }
    

    This QWidget "window2" has another QPushButton

    void window2::on_pushButton_clicked() {
    
          *abc = ui->plainTextEdit->toPlainText();
    
    }
    

    "*abc" is a QString and is declared in the global scope in main.cpp

    I assume I have to use some sort of connect() on either MainWindow, or window2. Which header file requires a "signal:"? MainWindow or window2?
    I think the basic function is "connect(object1, signal, object2, slot)" Where must connect() be declared? My QPushButtons are through QT creator in a .ui file, do I need to create a push button object for connect()? What objects do I need to connect? The Push Button and QTextBrowser? Or MainWindow? I can access these objects through

    void MainWindow::update_textBrowser() {
        ui->textBrowser->setText(*abc);
        ui->textBrowser->reload();
    
    }
    

    I would like to call this function after clicking window2's QPushButton. Must I use emit through window2's QPushButton?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      First thing: why are you using a global QString variable ?
      What is the goal of your application ?
      What is the goal of that Window2 widget ?

      Since you wrote you are new to C++, are you aware the the management of your objects lifetime ? From the looks of it, your Window2 instance is going to leak.

      That may be a lot of questions but getting the basics right before going with GUI application is very important.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        First thing: why are you using a global QString variable ?
        What is the goal of your application ?
        What is the goal of that Window2 widget ?

        Since you wrote you are new to C++, are you aware the the management of your objects lifetime ? From the looks of it, your Window2 instance is going to leak.

        That may be a lot of questions but getting the basics right before going with GUI application is very important.

        D Offline
        D Offline
        desun_evan
        wrote on last edited by
        #3

        @SGaist Okay, I will look into these topics. I am aware of constructor and deconstructor, and the new and delete keywords. My goal was to get user input from a QPlainTextEdit and update textBrowser from a button in window2 that would close the window and update any text the user has input. I will look into some basics, I couldn't find a place for QString variable where MainWindow and window2 could access it. Sorry for the newbie question, I figure even if I have a leak, I would learn about connect(), I might be doing this backwards so to speak...

        Thanks a lot!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So basically you will have a dialog to query some data from your user. Then the usual way is to show the dialog in a blocking manner and then grab the data from it. Check QDialog and QInputDialog.

          You should also write down how your application shall work.

          Sharing data between widgets does not mean using global state variable but designing data sharing properly.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

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