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. [SOLVED] Change QStackedLayout Index with QPushButton not QComboBox
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Change QStackedLayout Index with QPushButton not QComboBox

Scheduled Pinned Locked Moved General and Desktop
21 Posts 4 Posters 8.3k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #8

    Dialog is the class name, not the instance, replace it by "this"

    Are you sure you initialized all your pointers ?

    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
    0
    • A Offline
      A Offline
      Ade Malsasa Akbar
      wrote on last edited by
      #9

      [quote author="b1gsnak3" date="1374820506"]-the program crashes because you do not have tombold either declared or is a unreferenced pointer... add tombold = new QPushButton("asd"); and it should work-

      you should add tombold to your header file just to be sure[/quote]

      Thank you. I have added it before I ask here. This is the code:

      @#ifndef MAINWINDOW_H
      #define MAINWINDOW_H

      #include <QtGui>

      class Dialog : public QDialog
      {
      Q_OBJECT

      public:
      Dialog();
      QVBoxLayout *mainlayout;
      QVBoxLayout *layouta;
      QVBoxLayout *layoutb;
      QPushButton *tombola;
      QPushButton *tombolb;
      QPushButton *tombolc;
      QPushButton *tombold;
      QFrame *framea;
      QFrame *frameb;
      QStackedLayout *stackia;

      public slots:
      void change_stack();

      private:

      };

      #endif // MAINWINDOW_H@

      [quote author="SGaist" date="1374826305"]Dialog is the class name, not the instance, replace it by "this"

      Are you sure you initialized all your pointers ?[/quote]

      Thank you. Oukay, my first code was using this. But fails. Then my code above shows us that I have done pointers initializing. Now, maybe a mistake on my header? What is my mistake? Thank you, all.

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

        @
        QVBoxLayout *mainlayout = new QVBoxLayout;
        QVBoxLayout *layouta = new QVBoxLayout;
        QVBoxLayout *layoutb = new QVBoxLayout;
        QPushButton *tombola = new QPushButton("A");
        QPushButton *tombolb = new QPushButton("B");
        QPushButton *tombolc = new QPushButton("C");
        QFrame *framea = new QFrame;
        QFrame *frameb = new QFrame;
        QStackedLayout *stackia = new QStackedLayout;
        @

        You are shadowing all your class members, that's the problem

        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
        0
        • A Offline
          A Offline
          Ade Malsasa Akbar
          wrote on last edited by
          #11

          Thank you. Umm, but what is shadowing? And what I should do to fix it?

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

            @
            class Foo {
            public:
            Foo();
            int bar;
            };

            Foo::Foo()
            {
            int bar = 0; << same name as the member variable = shadowing
            << member variable is not initialized
            }
            @

            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
            0
            • A Offline
              A Offline
              Ade Malsasa Akbar
              wrote on last edited by
              #13

              [quote author="SGaist" date="1374869962"]@
              class Foo {
              public:
              Foo();
              int bar;
              };

              Foo::Foo()
              {
              int bar = 0; << same name as the member variable = shadowing
              << member variable is not initialized
              }
              @[/quote]
              Thank you, but what should I do to implement your sample code? Truly I am new in Qt and I don't understand.

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

                It's nothing Qt related, it's basic c/c++ knowledge. My example only shows what is happening in your code.

                @
                Foo::Foo()
                {
                bar = 0; << initializes the class member
                }
                @

                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
                0
                • A Offline
                  A Offline
                  Ade Malsasa Akbar
                  wrote on last edited by
                  #15

                  Oukay, so now, what should I do to fix my code?

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

                    Compare my two examples and apply the difference to your code.

                    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
                    0
                    • D Offline
                      D Offline
                      dbzhang800
                      wrote on last edited by
                      #17

                      Hi, you need to make yourself familiar with C++ ASAP.
                      [quote author="Ade Malsasa Akbar" date="1374967552"]Oukay, so now, what should I do to fix my code?[/quote]

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Ade Malsasa Akbar
                        wrote on last edited by
                        #18

                        Thank you, but my problem actually quite simple. I just don't know what to do in one piece of field. Can you give a clue?

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Ade Malsasa Akbar
                          wrote on last edited by
                          #19

                          SOLVED!

                          Now I can change the stack index, and the layer is changing by pusbutton click. The code is:

                          HEADER

                          @#ifndef MAINWINDOW_H
                          #define MAINWINDOW_H

                          #include <QtGui>

                          class Dialog : public QDialog
                          {
                          Q_OBJECT

                          public:
                          Dialog();
                          QVBoxLayout *mainlayout; //deklarasi objek-objek seharusnya dilakukan di sini saja
                          QVBoxLayout *layouta; //nanti di mainwindow.cpp hanya diulang deklarasinya tanpa nama kelas awal
                          QVBoxLayout *layoutb;
                          QPushButton *tombola;
                          QPushButton *tombolb;
                          QPushButton *tombolc;
                          QPushButton *tombold;
                          QFrame *framea;
                          QFrame *frameb;
                          QStackedLayout *stackia;

                          public slots:
                          void change_stack(); //harus public, kalau private maka stack tidak berganti sama sekali

                          private:
                          //ini dibiarkan kosong saja
                          };

                          #endif // MAINWINDOW_H@

                          CPP

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

                          Dialog::Dialog()
                          {
                          mainlayout = new QVBoxLayout; //kesalahan kode sehingga tidak bisa berubah indeks stack-nya
                          layouta = new QVBoxLayout; //adalah gara-gara dideklarasikan ulang nama kelasnya
                          layoutb = new QVBoxLayout; //contoh yang SALAH: QVBoxLayout *layout = new QVBoxLayout;
                          tombola = new QPushButton("A"); //contoh yang BENAR: layout = new QVBoxLayout;
                          tombolb = new QPushButton("B"); //dan harus dideklarasikan juga slot bertipe void di header
                          tombolc = new QPushButton("C"); //dan slot itu harus bertipe public tidak boleh private
                          framea = new QFrame;
                          frameb = new QFrame;
                          stackia = new QStackedLayout;

                          layouta->addWidget(tombola);
                          layoutb->addWidget(tombolb);
                          
                          framea->setLayout(layouta);
                          frameb->setLayout(layoutb);
                          framea->setMinimumSize(88,88);
                          frameb->setMinimumSize(88,88);
                          
                          //building frame
                          framea->setFrameShape(QFrame::StyledPanel);
                          framea->setFrameShadow(QFrame::Raised);
                          frameb->setFrameShape(QFrame::StyledPanel);
                          frameb->setFrameShadow(QFrame::Raised);
                          
                          //get c button smaller
                          tombolc->setMaximumWidth(33);
                          
                          stackia->addWidget(framea);
                          stackia->addWidget(frameb);
                          stackia->addWidget(tombolc);
                          
                          mainlayout->addLayout(stackia);
                          QPushButton     *tombold    =   new QPushButton("D");
                          mainlayout->addWidget(tombold);
                          setLayout(mainlayout);
                          
                          connect(tombold, SIGNAL(clicked()), this, SLOT(change_stack()));
                          

                          }

                          void Dialog::change_stack()
                          {
                          stackia->setCurrentIndex(a);
                          }
                          @

                          My mistakes:

                          I repeated the declaration in HEADER into CPP. The repeated declaration (the wrong one) is something like *QPushButton button = new QPushButton; and the correct one is something like button = new QPushButton;. I dont know what is the name for this but i don't care, my problem solved.

                          I dont know the correct code for my change_stack() function. The code for that should call setCurrentIndex() function for stack objeck.

                          I don't know the correct receiver and SLOT for my code. The correct one is what appeared in the code above.

                          The Best Answer

                          Thank you, Mr SGaist. My friend from Indonesia, kcin, tells me what is the meaning of your answer. Actually I don't understand. But after he says difference between int n = 1 and n = 1, I change my code and boom. Solved. Your clue was true. Thank you.

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

                            You're welcome, like 1+1=2 suggested, get a good book about C++. You'll understand better what your problems were.

                            Also, "it works so I don't care to understand" is a very bad philosophy, even more when programming in C++, this will result in poor/bad code that will bite you back at the worse time.

                            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
                            0
                            • A Offline
                              A Offline
                              Ade Malsasa Akbar
                              wrote on last edited by
                              #21

                              Thank you, but my religion forbid me hardly from philosophy. But your suggestion to learn is good. I accept 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