Results 1 to 11 of 11

Thread: how to change 1st mainwindow's frame by 2nd window's frame

  1. #1
    Join Date
    Dec 2018
    Posts
    6
    Qt products
    Qt4 Qt5

    Default how to change 1st mainwindow's frame by 2nd window's frame

    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:

    Qt Code:
    1. .h
    2. #ifndef FIRSTDIALOG_H
    3. #define FIRSTDIALOG_H
    4.  
    5. #include <QDialog>
    6. #include <QFrame>
    7. #include <QWidget>
    8. #include <QListWidgetItem>
    9.  
    10. namespace Ui {
    11. class FirstDialog;
    12. }
    13.  
    14. class FirstDialog : public QDialog
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit FirstDialog(QWidget *parent = 0);
    20. ~FirstDialog();
    21.  
    22. private:
    23. Ui::FirstDialog *ui;
    24. QFrame *frame_4;
    25. };
    26.  
    27. #endif // FIRSTDIALOG_H
    28.  
    29. //---------------------------------------------
    30. .cpp
    31. #include "firstdialog.h"
    32. #include "ui_firstdialog.h"
    33. #include <QWidget>
    34.  
    35. FirstDialog::FirstDialog(QWidget *parent) :
    36. QDialog(parent),
    37. ui(new Ui::FirstDialog)
    38. {
    39. ui->setupUi(this);
    40. }
    41.  
    42. FirstDialog::~FirstDialog()
    43. {
    44. delete ui;
    45. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. .h
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QMainWindow>
    6. #include <QLabel>
    7. //#include <QListWidget>
    8. //#include <QWidget>
    9. #include <QListWidgetItem>
    10.  
    11. namespace Ui {
    12. class MainWindow;
    13. }
    14.  
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit MainWindow(QWidget *parent = 0);
    21. ~MainWindow();
    22.  
    23. private slots:
    24. void on_pushButton_1_clicked();
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28. QFrame *frame_1;
    29. QFrame *frame_2;
    30. QFrame *frame_3;
    31. };
    32.  
    33. #endif // MAINWINDOW_H
    34. //-------------------------------------------
    35. .cpp
    36. #include "mainwindow.h"
    37. #include "ui_mainwindow.h"
    38. #include "firstdialog.h"
    39. #include <QLabel>
    40. #include <QPixmap>
    41.  
    42. MainWindow::MainWindow(QWidget *parent) :
    43. QMainWindow(parent),
    44. ui(new Ui::MainWindow)
    45. {
    46. ui->setupUi(this);
    47. QPixmap pix("C:/Users/Infonet/Documents/frames_test/logo6_DT.png");
    48. int w = ui->label_1->width();
    49. int h = ui->label_1->height();
    50. ui->label_1->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));
    51. }
    52.  
    53. MainWindow::~MainWindow()
    54. {
    55. delete ui;
    56. }
    57.  
    58. void MainWindow::on_pushButton_1_clicked()
    59. {
    60. FirstDialog *firstdia = new FirstDialog;
    61. firstdia->show();
    62. }
    To copy to clipboard, switch view to plain text mode 

    thanks.
    Last edited by doubw; 10th December 2018 at 08:00.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Dec 2018
    Posts
    6
    Qt products
    Qt4 Qt5

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Dec 2018
    Posts
    6
    Qt products
    Qt4 Qt5

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. // ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Dec 2018
    Posts
    6
    Qt products
    Qt4 Qt5

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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 and learn the basics of how to use Qt and look at the examples and tutorials.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Dec 2018
    Posts
    6
    Qt products
    Qt4 Qt5

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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.

    firstdialog.ui

    #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();
    }

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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 for guidance.
    Attached Files Attached Files
    Last edited by ChrisW67; 16th December 2018 at 04:56.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  11. #11
    Join Date
    Dec 2018
    Posts
    6
    Qt products
    Qt4 Qt5

    Default Re: how to change 1st mainwindow's frame by 2nd window's frame

    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.

Similar Threads

  1. Replies: 1
    Last Post: 5th December 2013, 07:46
  2. Frame-by-frame event handling?
    By QTNovice in forum Qt Programming
    Replies: 1
    Last Post: 28th October 2013, 23:32
  3. Video Parsing - Frame by Frame
    By ctote in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2010, 19:30
  4. Previous frame inner to this frame(corrupt stack?)
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 28th May 2007, 02:35
  5. Previous frame inner to this frame(corrupt stack?)
    By coralbird in forum Qt Programming
    Replies: 17
    Last Post: 29th April 2006, 02:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.