Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. Perchè QPushButton occupa tutta la centralwidget?
Forum Updated to NodeBB v4.3 + New Features

Perchè QPushButton occupa tutta la centralwidget?

Scheduled Pinned Locked Moved Unsolved Italian
4 Posts 2 Posters 797 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
    Stefanoxjx
    wrote on last edited by
    #1

    Ciao a tutti,
    premetto che di Qt sono abbastanza inesperto ma in qualche modo in genere mi arrangio.
    Questa volta ho un problema che per voi sarà sicuramente semplice, ma io non riesco a capire.
    Il problema è che vorrei inserire dei widget nella mainwindow dall'interno di uno slot.
    Sono riuscito ad inserire un QPushButton, ma mi occupa l'intera finestra nonostante abbia dato delle dimensioni precise.

    Di seguito uno stralcio del codice:
    Costruttore:

    mw::mw(QWidget *parent) : QMainWindow(parent)
    {
        //Fisso la dimensione della finestra e la centro sullo schermo
        QScreen *screen = QGuiApplication::primaryScreen();
        QRect  screenGeometry = screen->geometry();
        this->setFixedSize(SCREENWIDTH, SCREENHEIGHT);
        this->setGeometry((screenGeometry.width()-SCREENWIDTH)/2, (screenGeometry.height()-SCREENHEIGHT)/2, SCREENWIDTH, SCREENHEIGHT);
        this->setWindowTitle(APPNAME+" - "+APPVERSION);
    
        //Creo i menù
        QMenuBar *pMenuBar = new QMenuBar(this);
        setMenuBar(pMenuBar);
    
        //Menù Parameters
        mParameters = new QMenu("Parametri", this);
        aNewParam = new QAction("Nuovo", this);
        aEditParam = new QAction("Edit", this);
        aSaveParam = new QAction("Salva", this);
        mParameters->addAction(aNewParam);
        mParameters->addAction(aEditParam);
        mParameters->addAction(aSaveParam);
        this->menuBar()->addMenu(mParameters);
        connect(aNewParam, SIGNAL(triggered()), this, SLOT(NewParam()));
        connect(aEditParam, SIGNAL(triggered()), this, SLOT(EditParam()));
        connect(aSaveParam, SIGNAL(triggered()), this, SLOT(SaveParam()));
    }
    

    e qui lo slot incriminato:

    void nw::NewParam()
    {
        qDebug() << "New";
       
       QPushButton *pb;
       pb = new QPushButton; //(this);
       pb->setText("Ciao");
       pb->setGeometry(10,10,100,100);
       setCentralWidget(pb);
    }
    

    Se il pushbutton lo inserisco nel costruttore viene visualizzato correttamente, mentre definito com'è all'interno di NewParam occupa l'intero schermo con il testo "Ciao" al centro della finestra.
    Cosa sto sbagliando?
    Grazie.

    JonBJ 1 Reply Last reply
    0
    • S Stefanoxjx

      Ciao a tutti,
      premetto che di Qt sono abbastanza inesperto ma in qualche modo in genere mi arrangio.
      Questa volta ho un problema che per voi sarà sicuramente semplice, ma io non riesco a capire.
      Il problema è che vorrei inserire dei widget nella mainwindow dall'interno di uno slot.
      Sono riuscito ad inserire un QPushButton, ma mi occupa l'intera finestra nonostante abbia dato delle dimensioni precise.

      Di seguito uno stralcio del codice:
      Costruttore:

      mw::mw(QWidget *parent) : QMainWindow(parent)
      {
          //Fisso la dimensione della finestra e la centro sullo schermo
          QScreen *screen = QGuiApplication::primaryScreen();
          QRect  screenGeometry = screen->geometry();
          this->setFixedSize(SCREENWIDTH, SCREENHEIGHT);
          this->setGeometry((screenGeometry.width()-SCREENWIDTH)/2, (screenGeometry.height()-SCREENHEIGHT)/2, SCREENWIDTH, SCREENHEIGHT);
          this->setWindowTitle(APPNAME+" - "+APPVERSION);
      
          //Creo i menù
          QMenuBar *pMenuBar = new QMenuBar(this);
          setMenuBar(pMenuBar);
      
          //Menù Parameters
          mParameters = new QMenu("Parametri", this);
          aNewParam = new QAction("Nuovo", this);
          aEditParam = new QAction("Edit", this);
          aSaveParam = new QAction("Salva", this);
          mParameters->addAction(aNewParam);
          mParameters->addAction(aEditParam);
          mParameters->addAction(aSaveParam);
          this->menuBar()->addMenu(mParameters);
          connect(aNewParam, SIGNAL(triggered()), this, SLOT(NewParam()));
          connect(aEditParam, SIGNAL(triggered()), this, SLOT(EditParam()));
          connect(aSaveParam, SIGNAL(triggered()), this, SLOT(SaveParam()));
      }
      

      e qui lo slot incriminato:

      void nw::NewParam()
      {
          qDebug() << "New";
         
         QPushButton *pb;
         pb = new QPushButton; //(this);
         pb->setText("Ciao");
         pb->setGeometry(10,10,100,100);
         setCentralWidget(pb);
      }
      

      Se il pushbutton lo inserisco nel costruttore viene visualizzato correttamente, mentre definito com'è all'interno di NewParam occupa l'intero schermo con il testo "Ciao" al centro della finestra.
      Cosa sto sbagliando?
      Grazie.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Stefanoxjx said in Perchè QPushButton occupa tutta la centralwidget?:

      setCentralWidget(pb)

      Scusi, no parlo itialiano :)

      QMainWindow => centralWidget => layout => layout.addWidget(pb)

      centralWidget needs a Q[H|V]BoxLayout, then add QPushButton to the layout.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Stefanoxjx
        wrote on last edited by
        #3

        @JonB said in Perchè QPushButton occupa tutta la centralwidget?:

        QMainWindow => centralWidget => layout => layout.addWidget(pb)

        Hi Jonb, and thanks for your answer.
        The solution that you have suggested me works fine :)

        The code

        this->layout.addWidget(pb);
        

        is ok :)
        Many thanks.

        Stefano

        JonBJ 1 Reply Last reply
        1
        • S Stefanoxjx

          @JonB said in Perchè QPushButton occupa tutta la centralwidget?:

          QMainWindow => centralWidget => layout => layout.addWidget(pb)

          Hi Jonb, and thanks for your answer.
          The solution that you have suggested me works fine :)

          The code

          this->layout.addWidget(pb);
          

          is ok :)
          Many thanks.

          Stefano

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @Stefanoxjx
          Bene :)

          Ma: layout. ? this->layout()->addWidget(pb). In case anyone else reads this.

          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