Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Accessing widgets from another form
Qt 6.11 is out! See what's new in the release blog

Accessing widgets from another form

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.9k 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.
  • Q Offline
    Q Offline
    qtTwist
    wrote on last edited by
    #1

    I'm pretty new to c++ and qt. I want to make a debug console which is accessible from my main window and all other dialogs in my application

    My debug console has an QPlainTextEdit widget on it here's the code for debugConsole.h:
    @#ifndef DEBUGCONSOLE_H
    #define DEBUGCONSOLE_H

    #include <QString>
    #include <QDialog>

    namespace Ui {
    class debugConsole;
    }

    class debugConsole : public QDialog
    {
    Q_OBJECT

    public:
    explicit debugConsole(QWidget *parent = 0);
    ~debugConsole();

    void sendDbgOutput(QString output);
    

    private:
    Ui::debugConsole *ui;
    };

    #endif // DEBUGCONSOLE_H@

    and here's the code for debugConsole.cpp:
    @#include "debugconsole.h"
    #include "ui_debugconsole.h"

    debugConsole::debugConsole(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::debugConsole)
    {
    Qt::WindowFlags flags = windowFlags() ;
    flags |= Qt::CustomizeWindowHint ;
    flags &= ~Qt::WindowCloseButtonHint ;
    setWindowFlags(flags);
    ui->setupUi(this);
    }

    debugConsole::~debugConsole()
    {
    delete ui;
    }

    void debugConsole::sendDbgOutput(QString output)
    {
    ui->debugOutput->appendPlainText(output);
    }
    @

    from my mainwindow.cpp I include debugConsole.h like such:
    @#include "debugconsole.h"@

    then, I do this here:
    @debugConsole *dbgConsole;@

    now when I try to call it like this, my program crashes:
    @void MainWindow::captureEnabled()
    {
    if(ui->captureSlider->value() == 1)
    {
    capturing = true;
    ui->captureLabel->setText("YES");
    ui->captureLabel->repaint();
    dbgConsole->sendDbgOutput("Capture Enabled");
    }
    else
    {
    capturing = false;
    ui->captureLabel->setText("NO");
    ui->captureLabel->repaint();
    }
    }@

    what am i doing wrong/not doing at all?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      @debugConsole *dbgConsole = new debugConsole(this);@
      instead of:
      @debugConsole *dbgConsole;@

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qtTwist
        wrote on last edited by
        #3

        ok, I know i'm going to look pretty stupid for this.

        but when I put that at the top of mainwindow.cpp

        I get the error, "invalid use of 'this' at top level"

        but, if i put it inside of any other function it won't let me use it?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AcerExtensa
          wrote on last edited by
          #4

          in your debugConsole constructor, QWidget pointer is needed as parent widget. I have assumed what you are instantiating debugConsole instance inside some kind of main widget class.
          @debugConsole::debugConsole(QWidget *parent)@

          You can create instance of debugConsole without setting parent QWidget pointer.
          @debugConsole *dbgConsole = new debugConsole(NULL);@

          God is Real unless explicitly declared as Integer.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qtTwist
            wrote on last edited by
            #5

            i tried that but, I get a microsoft visual c++ runtime error

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AcerExtensa
              wrote on last edited by
              #6

              bq. i tried that but, I get a microsoft visual c++ runtime error

              It has nothing todo with debugConsole constructor... Code which causes program to fail is somewhere else...

              @debugConsole::debugConsole(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::debugConsole)
              {
              Qt::WindowFlags flags = windowFlags() ;
              flags |= Qt::CustomizeWindowHint ;
              flags &= ~Qt::WindowCloseButtonHint ;
              setWindowFlags(flags);
              ui->setupUi(this);
              }@

              God is Real unless explicitly declared as Integer.

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qtTwist
                wrote on last edited by
                #7

                ahhh, well thanks..

                Instead of reading a book I decided to jump right into programming and ask for help when I get stuck, so I'm sorry for being kind of an idiot.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  AcerExtensa
                  wrote on last edited by
                  #8

                  No problem... There's nothing shameful about being a beginner....

                  God is Real unless explicitly declared as Integer.

                  1 Reply Last reply
                  0
                  • Jimmy CrabJ Offline
                    Jimmy CrabJ Offline
                    Jimmy Crab
                    wrote on last edited by
                    #9

                    My solution is to make "ui" public:

                    //sub window: Form3PType1
                    public:
                        Ui::Form3PType1 *ui;
                    
                    //main window: FormCL3P
                    qDebug() << "ANo: " << G::form3PType1->ui->tbxANo->toPlainText();
                    
                    

                    output: ANo: "A5"

                    When the "ui" of a window is public, it can be accessed from other cpp.
                    Works for me.

                    Just do it.

                    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