[SOLVED] Change QStackedLayout Index with QPushButton not QComboBox
-
Thank you. Umm, but what is shadowing? And what I should do to fix it?
-
@
class Foo {
public:
Foo();
int bar;
};Foo::Foo()
{
int bar = 0; << same name as the member variable = shadowing
<< member variable is not initialized
}
@ -
[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. -
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
}
@ -
Oukay, so now, what should I do to fix my code?
-
Compare my two examples and apply the difference to your code.
-
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] -
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?
-
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_OBJECTpublic:
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 sekaliprivate:
//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.
-
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.
-
Thank you, but my religion forbid me hardly from philosophy. But your suggestion to learn is good. I accept it.