PDA

View Full Version : how to resize Qmainwindow with Qwidget ?



zeynepb.bil
5th August 2010, 12:05
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.



//main.cpp
#include "window.h" //the object inherited by Qdialog
#include "mainwindow.h" //the object inherited by Qmainwindow

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window box;
MainWindow window;
window.setParent(&box);

return app.exec();
}

//window.cpp
Window::Window()
{
createActions();
createTrayIcon();
setIcon();
/*..*/

trayIcon->show();
setWindowTitle(tr("Blog Search"));
resize(600, 600);
}
MainWindow::MainWindow()
{
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
int randomValue = qrand() % 999;
view = new QWebView(this);

QUrl url = QUrl::fromEncoded("http://blogsearch.google.com/blogsearch?hl=tr&ie=UTF-8&q=C%2B%2B&&output=rss");
url.addQueryItem("num", "1");
url.addQueryItem("start" ,QString::number(randomValue));
view->load(url);

/* ...... */

setCentralWidget(view);
}
/*
all the functions here
*/


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

Lykurg
5th August 2010, 12:14
You have to put MainWindow into the layout of Window.

zeynepb.bil
5th August 2010, 12:18
I'm new in Qt. So can you explain more ? If I add a new window to apllication am I alway use a layout ?

Lykurg
5th August 2010, 12:22
Since I don't understand your design exactely, please provide a minimal compilable example reproducing your problem.

zeynepb.bil
5th August 2010, 12:33
//main.cpp
#include <QtGui>

#include "window.h"
#include "mainwindow.h"

int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(systray);

QApplication app(argc, argv);
QApplication::setQuitOnLastWindowClosed(false);

Window box;

MainWindow window;
window.setParent(&box);
return app.exec();
}

//mainwindow.cpp
#include <QtGui>

class QWebView;


//! [1]
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

private:
QString jQuery;
QWebView *view;

};

//window.h
#ifndef WINDOW_H
#define WINDOW_H

#include <QDialog>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QLineEdit>
#include <QTextEdit>


class Window : public QDialog
{
Q_OBJECT
public:
Window();

private slots:
void setIcon();
void iconActivated(QSystemTrayIcon::ActivationReason reason);
void showMessage();

private:
void createActions();
void createTrayIcon();


QAction *minimizeAction;
QAction *maximizeAction;
QAction *restoreAction;
QAction *quitAction;
QLineEdit *titleEdit;
QTextEdit *bodyEdit;

QSystemTrayIcon *trayIcon;
QMenu *trayIconMenu;


};

#endif

//window.cpp

#include <QtGui>
#include <QtWebKit>
#include <QTime>

#include "window.h"
#include "mainwindow.h"

Window::Window()
{
createActions();
createTrayIcon();
setIcon();

connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason )),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReas on)));


trayIcon->show();
setWindowTitle(tr("Blog Search"));
resize(600, 600);
}
MainWindow::MainWindow()
{

QFile file;
file.setFileName(":/jquery.min.js");
file.open(QIODevice::ReadOnly);
jQuery = file.readAll();
file.close();


QNetworkProxyFactory::setUseSystemConfiguration(tr ue);


QTime time = QTime::currentTime();
qsrand((uint)time.msec());
int randomValue = qrand() % 999;
view = new QWebView(this);

QUrl url = QUrl::fromEncoded("http://blogsearch.google.com/blogsearch?hl=tr&ie=UTF-8&q=C%2B%2B&&output=rss");
url.addQueryItem("num", "1");
url.addQueryItem("start" ,QString::number(randomValue));
view->load(url);


setCentralWidget(view);
}

void Window::setIcon()
{
QIcon icon;
icon.addPixmap(QPixmap(":/images/heart.svg"));
trayIcon->setIcon(icon);
trayIcon->setVisible(true);
setWindowIcon(icon);

}
void Window::iconActivated(QSystemTrayIcon::ActivationR eason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
break;
case QSystemTrayIcon::DoubleClick:
showNormal();
break;
case QSystemTrayIcon::MiddleClick:
showMessage();
break;
default:
;
}
}

void Window::showMessage()
{
titleEdit = new QLineEdit(tr("Blog Search"));
bodyEdit = new QTextEdit;
bodyEdit->setPlainText(trUtf8("Blog okumak icin sag click + Maximize\n yapmaniz yeterli"));

trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), QSystemTrayIcon::Information,
1000);
}

void Window::createActions()
{
minimizeAction = new QAction(tr("Mi&nimize"), this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

maximizeAction = new QAction(tr("Ma&ximize"), this);
connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));

restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void Window::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);

trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}

Lykurg
5th August 2010, 12:44
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.

zeynepb.bil
5th August 2010, 13:02
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.

Lykurg
5th August 2010, 13:08
Of course you should use QMainWindow, but why you then make another window called window? Should there be two windows?

zeynepb.bil
5th August 2010, 13:22
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. :)

thanhpham288
28th September 2017, 04:11
I'm a new QT. have same issue with @zeynepb.bil
https://i.imgur.com/WEBkimQ.png

Can you help me?

d_stranz
28th September 2017, 23:48
Put everything that is inside the QWidget "centralWidget" inside a layout of some sort. Look at the Qt Layout examples (https://doc.qt.io/qt-5/examples-layouts.html) and follow the link on that page to Layout Management for the explanation of how it all works.