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. Avoiding ui->setupUi(this);
Forum Updated to NodeBB v4.3 + New Features

Avoiding ui->setupUi(this);

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 6.1k 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.
  • S Offline
    S Offline
    santiagorf
    wrote on 19 Sept 2012, 20:10 last edited by
    #1

    Hi I have the following files

    A form mainwindow.ui that contains a button named okButton

    main.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow){
    ui->setupUi(this);
    connect(ui->okButton,SIGNAL(clicked()),this,SLOT(accept()));

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    main.h
    @#include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }@

    Every time I want to acces and object from the form, I have to do it, as in the mainWindow constructor via ui->okButton. What changes do I need to do in order to access the okButton without having to use the ui pointer?

    Thanks in advance
    santiagorf

    1 Reply Last reply
    0
    • T Offline
      T Offline
      twsimpson
      wrote on 19 Sept 2012, 20:14 last edited by
      #2

      You can "inherit from the UI struct":http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html#the-multiple-inheritance-approach, so it's members become part of your class

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on 20 Sept 2012, 03:30 last edited by
        #3

        ... or you can construct your user interface using code you write for yourself.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 20 Sept 2012, 07:53 last edited by
          #4

          ...or you can add a QPushButton* member variable in your class, and assign the ui->okButton to that variable directly after the call to setupUi.

          ...or you can learn to appreciate that your UI elements are all nicely grouped together in a ui member instead of cluttering up your internal API.

          1 Reply Last reply
          0

          1/4

          19 Sept 2012, 20:10

          • Login

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