PDA

View Full Version : how to change 1st mainwindow's frame by 2nd window's frame



doubw
10th December 2018, 06:53
Hi,

i'm new in qt. and i have a minwindow and a second window. the 1st window has 3 frames and the 2nd has only one. my question: how to swap after clicked a button the 2nd window's frame with the 3rd frame of the 1st window? here are my codes:



.h
#ifndef FIRSTDIALOG_H
#define FIRSTDIALOG_H

#include <QDialog>
#include <QFrame>
#include <QWidget>
#include <QListWidgetItem>

namespace Ui {
class FirstDialog;
}

class FirstDialog : public QDialog
{
Q_OBJECT

public:
explicit FirstDialog(QWidget *parent = 0);
~FirstDialog();

private:
Ui::FirstDialog *ui;
QFrame *frame_4;
};

#endif // FIRSTDIALOG_H

//---------------------------------------------
.cpp
#include "firstdialog.h"
#include "ui_firstdialog.h"
#include <QWidget>

FirstDialog::FirstDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::FirstDialog)
{
ui->setupUi(this);
}

FirstDialog::~FirstDialog()
{
delete ui;
}




.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
//#include <QListWidget>
//#include <QWidget>
#include <QListWidgetItem>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_1_clicked();

private:
Ui::MainWindow *ui;
QFrame *frame_1;
QFrame *frame_2;
QFrame *frame_3;
};

#endif // MAINWINDOW_H
//-------------------------------------------
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "firstdialog.h"
#include <QLabel>
#include <QPixmap>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix("C:/Users/Infonet/Documents/frames_test/logo6_DT.png");
int w = ui->label_1->width();
int h = ui->label_1->height();
ui->label_1->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));
}

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

void MainWindow::on_pushButton_1_clicked()
{
FirstDialog *firstdia = new FirstDialog;
firstdia->show();
}


thanks.

d_stranz
10th December 2018, 18:06
What do you mean "swap frames"? I am not sure you understand what QFrame is or how to create one and use it in your UI. The code you have posted simply declares pointers to QFrame instances. It doesn't create the QFrame instances, and doesn't insert them anywhere into the UI.

doubw
12th December 2018, 18:18
hi,
yes, you've right. but i drawed in qt gui and when iclick to pushbutton1, it opens the firstdialog's window separately. what i want is open this firstdialog window (it's frame) at the frame3 of mainwindow. but i didn't writed code for that. ok i'll tray and i com back.
thanks

d_stranz
12th December 2018, 20:52
The "QFrame * frame_1" that you declare in your MainWindow class is not the same as the "frame_1" widget you create in the UI file. You do understand that, right?

doubw
13th December 2018, 18:04
Hi,
i can't understand that pls. why they are not the same? i draged from the qt designer as qframe and declared in mainwindow as qframe class.
thanks.

d_stranz
13th December 2018, 23:29
The QFrame variable is declared in your UI file ui_MainWindow.h. This file is automatically created by Qt's UIC compiler from the MainWindow.ui file, which you have #include-d in MainWindow.cpp. That QFrame is a member variable in the Ui::MainWindow class. You create an instance of that class in your MainWindow constructor:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// ...
}


and the call to ui->setupUi() creates that QFrame. There is no code in your MainWindow class that creates the QFrame instances you declare in MainWindow itself.

If you still don't understand why the QFrame * frame_1 in MainWindow is not the same as the QFrame * frame_1 in Ui::MainWindow, you need to go back to your C++ book and read about namespaces.

doubw
14th December 2018, 12:56
Hi
this isn't a good idea saying to somone: you know nothing!! of corse, if i knew all, i wouldn't be in this forum. i want from you to gide me to something like qstackedwidget.. i would like to understand little about qt framework!
thanks

d_stranz
14th December 2018, 16:24
We are here to help with specific Qt programming problems, not to teach basic C++. I am sorry if this sounds rude, but you really -do- need to understand C++ programming in order to use Qt at all. The code that you posted shows that you have problems with some really basic concepts, like the difference between declaring a pointer variable as a member of a C++ class and how that pointer variable is assigned and used, and not understanding that two variables with the same name but in different namespaces are not the same variable..

I will admit that when I first started out with Qt about 10 - 12 years ago, I was puzzled about Qt classes and UI files. There is a lot of magic that happens inside of Qt at different times - when you design a UI using Qt Designer, when you build your project using Qt Creator or Visual Studio, and when you run it. Understanding what happens and when is important to be able to use Qt as part of your applications.

Qt is supported by a huge number of examples, tutorials, and other documentation. I would suggest you start with this (https://www.qt.io/developers/) and learn the basics of how to use Qt and look at the examples and tutorials.

doubw
15th December 2018, 13:03
you are not interesting with your behaviour! this code you
insulting is generated by the qt designer. it isn't my code!!
i'm beginner you know and i'm understanding examples. this code
is working perfectly. i forgot to send the "firstdialog.ui" and
the "main()" function. when i click to the pushbutton, the
firstdialog window is appearing. what i wanted is that it apears
in the place of frame_3 of the mainwindow. but you are giving
responses like you want in this good forum(i think). this is my
last response for you,i wasted my time with somebody like you.
here are these 2 functions i forgot.

12987



#include <QApplication>
#include "mainwindow.h"

int main(int argc, char* argv[])
{
QApplication app(argc,argv);
MainWindow w;
w.setFixedSize(800,700);
w.show();

return app.exec();
}

ChrisW67
16th December 2018, 03:47
You have a QMainWindow and a QDialog. Both of these are intended to create a separate window on the desktop when show() (or exec()) is called... I assume this is what you see. Instead, when you click a button in the main window you want the content that is displayed in the current dialog to be shown as part of the content area of the main window. It's not clear whether that will be:
1. Replacing existing content in the "third frame". You generally use a QStackedWidget to show alternate widgets in the same layout space.
2. The "third frame" is normally hidden and only shown, with the dialog's contents, on demand.

Case 1 is demonstrated in the attached example based on yours. I mocked up the main window content. Flip between options in the stack using the two push buttons.
I changed your FirstDialog class to QWidget because it really is not a dialog. It is embedded in the main window designer ui by promoting page 2 of the stacked widget.
You will notice that the QFrame* member variables you put in the two headers are neither present nor needed.

Case 2 is handled differently and has more complicated implications for layout handling. Take a look at the Qt Extension Example (http://doc.qt.io/qt-5/qtwidgets-dialogs-extension-example.html) for guidance.

doubw
16th December 2018, 07:23
Hi,
first of all, I ask you to excuse me all (even d_stranz). i was giving up qt. i'm doing good things in c and a little in c++. i'm thinking qt is good and well designed. i come back to read all and will be back. thanks a lot chrisw67.