PDA

View Full Version : Newbie dealing with statusbar incluiding widgets.



chavetaz
5th July 2010, 02:46
Hi,
Im newbie in QT and C++...
Im trying to add widgets (label and progressbar) to statusbar and found 2 ways (I think...):

1- create the widgets and add them to statusbar in main class. this works OK!! but I dont know how to update the progressbar within mainwindow class... I stopped here... :confused::confused:

2- I see in this forum: create the widgets and add them to statusbar in mainwindow class (maybe is more simple to access to update values) but i doesnt work... I stopped here.... :confused::confused:

Anyone please assist me?

I post the code of my second option (may be the best???):

Mainwindow.h :


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QUrl>
#include <QMap>
#include <QProgressBar>
#include <QHBoxLayout>
#include <QLabel>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private:
void init();
void createStatusBar();

Ui::MainWindow *ui;
QMap<QString, int> Globales;
// QFrame status_frame;
// QHBoxLayout layout;
QProgressBar *bar;
QLabel *box;

private slots:
void on_Web_loadProgress(int progress);
void on_Web_urlChanged(QUrl);
void on_Web_loadStarted();
void on_BtnLogin_toggled(bool checked);
};

#endif // MAINWINDOW_H


mainwindow.cpp :


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebSettings>
#include <QProgressBar>
#include <QLabel>
#include <QStatusBar>
#include <QProgressBar>


MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
// Rutinas de Inicio
createStatusBar();
init();

ui->setupUi(this);

}

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

void MainWindow::on_BtnLogin_toggled(bool checked)
{
if (checked) {
//login
Globales["Estado"] = 0;
ui->Web->load(QUrl(ui->txtURL->text()));
}
else{
//logout.
ui->Web->load(QUrl("http://..."));
Globales["Estado"] = 0;
}
}

void MainWindow::on_Web_loadStarted()
{
// here I intent to show the progressbar
//ui->statusBar->pBar->show();
}

void MainWindow::on_Web_urlChanged(QUrl newurl)
{
ui->LogNav->setTextColor(QColor("blue"));
ui->LogNav->append(newurl.toString());
}

void MainWindow::init()
{
//Fijar seteos del browser
QWebSettings::globalSettings()->setAttribute(QWebSettings::AutoLoadImages, false);

}

void MainWindow::on_Web_loadProgress(int progress)
{
// Here I intent to update the progressbar
}

void MainWindow::createStatusBar()
{
// status_frame = new QFrame();
// status_frame->setFrameStyle(QFrame::Panel | QFrame::Sunken);

// layout = new QHBoxLayout(status_frame);
// layout->setContentsMargins(0, 0, 0, 0);

bar = new QProgressBar();
bar->setMaximumHeight(10);
bar->setMaximumWidth(100);
bar->setValue(80); //for testing

box = new QLabel("Check Mode");

// Two Options: Which is right ???
//ui->statusBar->addWidget(bar);
//ui->statusBar->addWidget(box);
statusBar()->addWidget(bar);
statusBar()->addWidget(box);
}


Best regards!!!

Ginsengelf
5th July 2010, 07:17
Hi, maybe it already helps to move setupUi() to be the first command in constructor of MainWindow.

Ginsengelf

PS: statusBar()-method should be Ok in createStatusBar(), in your pasted code the other method should not work or even crash because ui is not set up when you call createStatusBar()

chavetaz
6th July 2010, 02:08
Hi Amigo!
it's works perfectly!!!

SetupUi() create the statusbar.... :D


Many Thanks ! ! !