PDA

View Full Version : Adding Separate Widgets into Layout



ashboi
26th May 2012, 14:10
I've created 3 separate widgets(3different classes), I would like to add them into my QMainwindow in the 3 frames that I've created using the Qt designer. I cant seem to get it to work here is my main file hopefully someone could tell me whats wrong with it.

the 3 different classes are secret, decode & pick.

Thank you



int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow *MyAppWindow = new QMainWindow;
Ui::MainWindow mainwindow;
mainwindow.setupUi(MyAppWindow);

mainwindow.secretFrame->secret(QWidget *parent);
mainwindow.boardFrame->decode(QWidget *parent);
mainwindow.pickerFrame->pick(QWidget *parent);

MyAppWindow->show();
return app.exec();
}

amleto
27th May 2012, 11:29
well, fixing syntax error give this...

mainwindow.secretFrame->secret(parent);
mainwindow.boardFrame->decode(parent);
mainwindow.pickerFrame->pick(parent);

but since you dont have any 'parent' variable, or show what secret(), decode() and pick() do, it's impossible to tell you how to fix your code (which we cant see).


if you want to add widget(s) to a frame, you could put them in a layout and add the layout to the frame

ashboi
27th May 2012, 11:57
here is one of my widgets

.h file


class Decode : public QWidget
{
public:
//constructor
Decode(QWidget *parent = 0);

//declaration of private methods
void decodepeg(QHBoxLayout *layout);
string peg;

protected:
void paintEvent(QPaintEvent *event); //PaintEvent

};

#endif

.cpp file


Decode::Decode(QWidget *parent):QWidget(parent)
{
QHBoxLayout *layout = new QHBoxLayout(parent);
decodepeg(layout);
decodepeg(layout);
decodepeg(layout);
decodepeg(layout);
}

void Decode::decodepeg(QHBoxLayout *layout)
{
string pegs;
Peg *peg = new Peg();
cout<<"Choose a color for the SecretCode Pegs"<<endl;
cin>>pegs;
peg->colour(pegs);
layout -> addWidget(peg);
}


I'm getting an error at this part


mainwindow.secretFrame->secret(QWidget *parent);
mainwindow.boardFrame->decode(QWidget *parent);
mainwindow.pickerFrame->pick(QWidget *parent);

of the main.cpp file

amleto
27th May 2012, 12:10
as I say - you get an error because that isnt valid c++.

Are you trying to call the constuctor directy? Because you wont be allowed to do that either.

ashboi
27th May 2012, 14:24
is there a way to insert the entire class as a widget to the layout?

amleto
27th May 2012, 15:07
yes.

QLayout and its specialisations. read the docs.

ChrisW67
28th May 2012, 04:46
I've created 3 separate widgets(3different classes), I would like to add them into my QMainwindow in the 3 frames that I've created using the Qt designer.
Then put them in the 3 frames you created in Designer using Designer. Drop a QWidget into each frame and promote it.

You really need to earn some basic C++ because lines 8 through 10 are not even close to being useful. Here are some questions to ask yourself:

What type is Ui::MainWindow::secretFrame, boardFrame or pickerFrame?
What functions and members are available in that type of object?
Do those members include one called decode(), secret() or pick()?
Where do you think the methods decode(), secret() or pick() are supposed to come from?
What do they have to do with your class Decode? Does it have a method decode()?
How would you pass a widget object to a decode() function if you had one?


As exercise you should read the code that uic generates from a simple Designer *.ui file.
Also read the documentation on ways to use a Designer Ui in your application especially the last paragraph of the "Direct Approach" section on the limits this imposes.