Results 1 to 11 of 11

Thread: how to resize Qmainwindow with Qwidget ?

  1. #1
    Join Date
    Aug 2010
    Location
    Turkey / Istanbul
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default how to resize Qmainwindow with Qwidget ?

    I have trayicon on Qdialog, also I have a webview on Qmainwindow. I want to do that a web page run like tray. The codes run perfectly but i have a problem with the window size. when I maximize the page on tray, the web page is smaller than qwidget.

    Qt Code:
    1. //main.cpp
    2. #include "window.h" //the object inherited by Qdialog
    3. #include "mainwindow.h" //the object inherited by Qmainwindow
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. Window box;
    9. MainWindow window;
    10. window.setParent(&box);
    11.  
    12. return app.exec();
    13. }
    14.  
    15. //window.cpp
    16. Window::Window()
    17. {
    18. createActions();
    19. createTrayIcon();
    20. setIcon();
    21. /*..*/
    22.  
    23. trayIcon->show();
    24. setWindowTitle(tr("Blog Search"));
    25. resize(600, 600);
    26. }
    27. MainWindow::MainWindow()
    28. {
    29. QTime time = QTime::currentTime();
    30. qsrand((uint)time.msec());
    31. int randomValue = qrand() % 999;
    32. view = new QWebView(this);
    33.  
    34. QUrl url = QUrl::fromEncoded("http://blogsearch.google.com/blogsearch?hl=tr&ie=UTF-8&q=C%2B%2B&&output=rss");
    35. url.addQueryItem("num", "1");
    36. url.addQueryItem("start" ,QString::number(randomValue));
    37. view->load(url);
    38.  
    39. /* ...... */
    40.  
    41. setCentralWidget(view);
    42. }
    43. /*
    44. all the functions here
    45. */
    To copy to clipboard, switch view to plain text mode 

    some of the codes above.
    Can you help me about what can I do for resize the mainwindow ?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to resize Qmainwindow with Qwidget ?

    You have to put MainWindow into the layout of Window.

  3. #3
    Join Date
    Aug 2010
    Location
    Turkey / Istanbul
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: how to resize Qmainwindow with Qwidget ?

    I'm new in Qt. So can you explain more ? If I add a new window to apllication am I alway use a layout ?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to resize Qmainwindow with Qwidget ?

    Since I don't understand your design exactely, please provide a minimal compilable example reproducing your problem.

  5. #5
    Join Date
    Aug 2010
    Location
    Turkey / Istanbul
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: how to resize Qmainwindow with Qwidget ?

    Qt Code:
    1. //main.cpp
    2. #include <QtGui>
    3.  
    4. #include "window.h"
    5. #include "mainwindow.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. Q_INIT_RESOURCE(systray);
    10.  
    11. QApplication app(argc, argv);
    12. QApplication::setQuitOnLastWindowClosed(false);
    13.  
    14. Window box;
    15.  
    16. MainWindow window;
    17. window.setParent(&box);
    18. return app.exec();
    19. }
    20.  
    21. //mainwindow.cpp
    22. #include <QtGui>
    23.  
    24. class QWebView;
    25.  
    26.  
    27. //! [1]
    28. class MainWindow : public QMainWindow
    29. {
    30. Q_OBJECT
    31.  
    32. public:
    33. MainWindow();
    34.  
    35. private:
    36. QString jQuery;
    37. QWebView *view;
    38.  
    39. };
    40.  
    41. //window.h
    42. #ifndef WINDOW_H
    43. #define WINDOW_H
    44.  
    45. #include <QDialog>
    46. #include <QSystemTrayIcon>
    47. #include <QMenu>
    48. #include <QLineEdit>
    49. #include <QTextEdit>
    50.  
    51.  
    52. class Window : public QDialog
    53. {
    54. Q_OBJECT
    55. public:
    56. Window();
    57.  
    58. private slots:
    59. void setIcon();
    60. void iconActivated(QSystemTrayIcon::ActivationReason reason);
    61. void showMessage();
    62.  
    63. private:
    64. void createActions();
    65. void createTrayIcon();
    66.  
    67.  
    68. QAction *minimizeAction;
    69. QAction *maximizeAction;
    70. QAction *restoreAction;
    71. QAction *quitAction;
    72. QLineEdit *titleEdit;
    73. QTextEdit *bodyEdit;
    74.  
    75. QSystemTrayIcon *trayIcon;
    76. QMenu *trayIconMenu;
    77.  
    78.  
    79. };
    80.  
    81. #endif
    82.  
    83. //window.cpp
    84.  
    85. #include <QtGui>
    86. #include <QtWebKit>
    87. #include <QTime>
    88.  
    89. #include "window.h"
    90. #include "mainwindow.h"
    91.  
    92. Window::Window()
    93. {
    94. createActions();
    95. createTrayIcon();
    96. setIcon();
    97.  
    98. connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
    99. this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
    100.  
    101.  
    102. trayIcon->show();
    103. setWindowTitle(tr("Blog Search"));
    104. resize(600, 600);
    105. }
    106. MainWindow::MainWindow()
    107. {
    108.  
    109. QFile file;
    110. file.setFileName(":/jquery.min.js");
    111. file.open(QIODevice::ReadOnly);
    112. jQuery = file.readAll();
    113. file.close();
    114.  
    115.  
    116. QNetworkProxyFactory::setUseSystemConfiguration(true);
    117.  
    118.  
    119. QTime time = QTime::currentTime();
    120. qsrand((uint)time.msec());
    121. int randomValue = qrand() % 999;
    122. view = new QWebView(this);
    123.  
    124. QUrl url = QUrl::fromEncoded("http://blogsearch.google.com/blogsearch?hl=tr&ie=UTF-8&q=C%2B%2B&&output=rss");
    125. url.addQueryItem("num", "1");
    126. url.addQueryItem("start" ,QString::number(randomValue));
    127. view->load(url);
    128.  
    129.  
    130. setCentralWidget(view);
    131. }
    132.  
    133. void Window::setIcon()
    134. {
    135. QIcon icon;
    136. icon.addPixmap(QPixmap(":/images/heart.svg"));
    137. trayIcon->setIcon(icon);
    138. trayIcon->setVisible(true);
    139. setWindowIcon(icon);
    140.  
    141. }
    142. void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
    143. {
    144. switch (reason) {
    145. case QSystemTrayIcon::Trigger:
    146. break;
    147. case QSystemTrayIcon::DoubleClick:
    148. showNormal();
    149. break;
    150. case QSystemTrayIcon::MiddleClick:
    151. showMessage();
    152. break;
    153. default:
    154. ;
    155. }
    156. }
    157.  
    158. void Window::showMessage()
    159. {
    160. titleEdit = new QLineEdit(tr("Blog Search"));
    161. bodyEdit = new QTextEdit;
    162. bodyEdit->setPlainText(trUtf8("Blog okumak icin sag click + Maximize\n yapmaniz yeterli"));
    163.  
    164. trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), QSystemTrayIcon::Information,
    165. 1000);
    166. }
    167.  
    168. void Window::createActions()
    169. {
    170. minimizeAction = new QAction(tr("Mi&nimize"), this);
    171. connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
    172.  
    173. maximizeAction = new QAction(tr("Ma&ximize"), this);
    174. connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
    175.  
    176. restoreAction = new QAction(tr("&Restore"), this);
    177. connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
    178.  
    179. quitAction = new QAction(tr("&Quit"), this);
    180. connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    181. }
    182.  
    183. void Window::createTrayIcon()
    184. {
    185. trayIconMenu = new QMenu(this);
    186. trayIconMenu->addAction(minimizeAction);
    187. trayIconMenu->addAction(maximizeAction);
    188. trayIconMenu->addAction(restoreAction);
    189. trayIconMenu->addSeparator();
    190. trayIconMenu->addAction(quitAction);
    191.  
    192. trayIcon = new QSystemTrayIcon(this);
    193. trayIcon->setContextMenu(trayIconMenu);
    194. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to resize Qmainwindow with Qwidget ?

    Ok, you don't use layouts at all. Please read the doc about layouts: http://doc.trolltech.com/4.6/layout.html. Also I don't see a reason why you have a widget and a main window. You can put all into one! If you need different widgets inside a main window see QStackedWidget.

  7. #7
    Join Date
    Aug 2010
    Location
    Turkey / Istanbul
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: how to resize Qmainwindow with Qwidget ?

    Quote Originally Posted by Lykurg View Post
    Ok, you don't use layouts at all. Please read the doc about layouts: http://doc.trolltech.com/4.6/layout.html. Also I don't see a reason why you have a widget and a main window. You can put all into one! If you need different widgets inside a main window see QStackedWidget.
    but I have to use QToolBar, so when I put all them in one widget an error occured. for example; addToolBar was not declared in this scope. That's why I prefer to use Qmainwindow.

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to resize Qmainwindow with Qwidget ?

    Of course you should use QMainWindow, but why you then make another window called window? Should there be two windows?

  9. #9
    Join Date
    Aug 2010
    Location
    Turkey / Istanbul
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: how to resize Qmainwindow with Qwidget ?

    Quote Originally Posted by Lykurg View Post
    Of course you should use QMainWindow, but why you then make another window called window? Should there be two windows?
    No, it should'nt. Firstly I tried it but can't do it one window then I did it like that. Now there is only one problem that; size of the window.
    If it will be easier to do it in one window okay then I try it to do in one window.

  10. #10
    Join Date
    May 2017
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: how to resize Qmainwindow with Qwidget ?

    I'm a new QT. have same issue with @zeynepb.bil
    https://i.imgur.com/WEBkimQ.png

    Can you help me?

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,111
    Thanks
    235
    Thanked 654 Times in 644 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: how to resize Qmainwindow with Qwidget ?

    Put everything that is inside the QWidget "centralWidget" inside a layout of some sort. Look at the Qt Layout examples and follow the link on that page to Layout Management for the explanation of how it all works.
    <=== 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.

Similar Threads

  1. QWidget on QMainWindow
    By jayreddy in forum Qt Programming
    Replies: 4
    Last Post: 6th January 2010, 08:27
  2. resize a QMainWindow
    By graciano in forum Newbie
    Replies: 4
    Last Post: 19th August 2009, 16:45
  3. Resize QWidget in QMainWindow
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2007, 12:16
  4. QMainWindow resize problem
    By edb in forum Qt Programming
    Replies: 5
    Last Post: 12th January 2007, 10:31
  5. How to resize a QMainWindow in QWorkspace.
    By s_a_white in forum Newbie
    Replies: 2
    Last Post: 17th July 2006, 18:22

Tags for this Thread

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.