Layout wont work after adding statusbar and menu bar feature to app (changing it to a QMainWindow)



  • hi , this is whole code :

    @#include <QVBoxLayout>
    #include <QPushButton>
    #include <QInputDialog>
    #include <QMenu>
    #include <QMenuBar>
    #include <QStatusBar>
    #include <QTime>
    #include "listwidget.h"

    ListWidget::ListWidget(QWidget *parent)
    : QMainWindow(parent)
    {
    startTimer(500);

    QVBoxLayout *vbox = new QVBoxLayout();
    QHBoxLayout *hbox = new QHBoxLayout(this);

    lw = new QListWidget(this);
    /lw->addItem("The Omen");
    lw->addItem("The Exorcist");
    lw->addItem("Notes on a scandal");
    lw->addItem("Fargo");
    lw->addItem("Capote");
    /

    //CB = new QCheckBox("Extreme Warnings",this);

    add = new QPushButton("Add", this);
    rename = new QPushButton("Rename", this);
    remove = new QPushButton("Remove", this);
    removeAll = new QPushButton("Remove All", this);

    ExtremeWarnings = new QAction("&ExtremeWarnings",this);
    ExtremeWarnings->setCheckable(true);

    Statusbar = new QAction("&StatusBar",this);
    Statusbar->setCheckable(true);

    QMenu *Options;
    Options = menuBar()->addMenu("&Options");
    Options->addAction(ExtremeWarnings);
    Options->addAction(Statusbar);

    statusBar();

    vbox->setSpacing(3);
    vbox->addStretch(1);
    vbox->addWidget(add);
    vbox->addWidget(rename);
    vbox->addWidget(remove);
    vbox->addWidget(removeAll);

    vbox->addStretch(1);

    hbox->addWidget(lw);
    hbox->addSpacing(5);
    hbox->addLayout(vbox);

    connect(add, SIGNAL(clicked()), this, SLOT(addItem()));
    connect(rename, SIGNAL(clicked()), this, SLOT(renameItem()));
    connect(remove, SIGNAL(clicked()), this, SLOT(removeItem()));
    connect(removeAll, SIGNAL(clicked()), this, SLOT(clearItems()));
    connect(ExtremeWarnings, SIGNAL(triggered(bool)), this
    , SLOT(ToggleExtremeeWarnings()));

    connect(Statusbar,SIGNAL(triggered(bool)), this
    , SLOT(EStatusbar()));

    setLayout(hbox);

    rename->setDisabled(true);
    remove->setDisabled(true);

    MB.addButton(QMessageBox::Yes);
    MB.addButton(QMessageBox::No);
    MB.setDefaultButton(QMessageBox::No);

    CB = false;
    }

    void ListWidget::addItem()
    {
    QString name = QInputDialog::getText(this, "Item",
    "Enter new item");
    name = name.simplified();
    if(!name.isEmpty()) {
    lw->addItem(name);
    lw->setCurrentRow(lw->count() - 1);
    }

    statusBar()->showMessage("Item " + name + " added",1000);
    

    }

    void ListWidget::renameItem()
    {
    if(lw->count() != 0){
    QListWidgetItem *curitem = lw->currentItem();
    int r = lw->row(curitem);
    QString text = curitem->text();
    QString item = QInputDialog::getText(this, "Item",
    "Enter new item", QLineEdit::Normal, text);

    item = item.simplified();
    
    if (!item.isEmpty()) {
        lw->takeItem(r);
        delete curitem;
        lw->insertItem(r, item);
        lw->setCurrentRow(r);
    }
    

    }
    statusBar()->showMessage("Item Renamed!",1000);
    }

    void ListWidget::removeItem()
    {
    if(CB){
    QString name = QInputDialog::getText(this, "Removing Item",
    "Type: DELETE to delete item");
    if(name == "DELETE")
    lw->takeItem(lw->row(lw->currentItem()));

    } else {
        MB.setText("Your about to remove " + lw->currentItem()->text());
        MB.setInformativeText("Are you sure?");
        int ret = MB.exec&#40;&#41;;
        if(lw->count() != 0 && ret == QMessageBox::Yes)
            lw->takeItem(lw->row(lw->currentItem()));
    }
    statusBar()->showMessage("Item Removed!",1000);
    

    }

    void ListWidget::clearItems()
    {
    if(CB){
    QString name = QInputDialog::getText(this, "Removing all",
    "Type: CLEAR to delete item");
    if(name == "CLEAR")
    lw->clear();

    } else {
    MB.setText("Your about Remove All items");
    MB.setInformativeText("Are you sure?");
    int ret = MB.exec&#40;&#41;;
    if(ret == QMessageBox::Yes)
        lw->clear();
    }
    statusBar()->showMessage("Cleared!",1000);
    

    }

    void ListWidget::timerEvent(QTimerEvent *e)
    {
    Q_UNUSED(e);

    if(lw->count() > 0)
    {
        rename->setDisabled(false);
        remove->setDisabled(false);
    } else {
        rename->setDisabled(true);
        remove->setDisabled(true);
    }
    

    }

    void ListWidget::ToggleExtremeeWarnings(bool is)
    {
    CB = is;
    statusBar()->showMessage("Extreme mod on, 2000");
    }

    void ListWidget::EStatusbar(bool is)
    {
    if(is)
    statusBar()->show();
    else
    statusBar()->hide();
    }
    @

    sorry its so long,

    this is header

    @#ifndef LISTWIDGET_H
    #define LISTWIDGET_H

    #include <QWidget>
    #include <QPushButton>
    #include <QListWidget>
    #include <QTextStream>
    #include <QCheckBox>
    #include <QMessageBox>
    #include <QMainWindow>
    #include <QApplication>

    class ListWidget : public QMainWindow
    {
    Q_OBJECT

    public:
    ListWidget(QWidget *parent = 0);

    private slots:
    void addItem();
    void renameItem();
    void removeItem();
    void clearItems();
    void ToggleExtremeeWarnings(bool);
    void EStatusbar(bool);

    private:
    QListWidget *lw;
    QPushButton *add;
    QPushButton *rename;
    QPushButton *remove;
    QPushButton *removeAll;
    //QCheckBox *CB;
    bool CB;
    QMessageBox MB;
    QAction *ExtremeWarnings;
    QAction *Statusbar;
    protected:
    void timerEvent(QTimerEvent *event);
    };

    #endif
    @



  • QMainWindow already has layout. So you can't set it. You can use the QMainWIndow directly and set the centralWidget holder in QMainWindow as using this.setCentralWidget(listWidget).

    Any reason you are inhering from QMainWindow ?



  • [quote author="Dheerendra" date="1403368003"]QMainWindow already has layout. So you can't set it. You can use the QMainWIndow directly and set the centralWidget holder in QMainWindow as using this.setCentralWidget(listWidget).

    Any reason you are inhering from QMainWindow ?[/quote]

    oh thanks , i didn't know that, i must re read the part about using QMainWindow to layout widgets again :) thanks,

    anyway

    i use it to be able to use status bar and menu bar:)

    btw my widgets wont scale when i make my window bigger or ect, i used rename->move(x,y); to put them in place !



  • is there anyway i can use Menubar with QWidgets (and use these layouts)?



  • You are using the ListWidget and MainWinow in Layout ? You can put the ListWidget directly in the centralwidget of MainWindow. Does it not solve your issue ?

    To answer your question QMenuBar is also Widget. So you can put the QMenuBar and Your widget in any layout.



  • You can modify the constructor like the following. You will be all set.

    @ListWidget::ListWidget(QWidget *parent)
    : QMainWindow(parent)
    {
    startTimer(500);
    ....................
    ....................
    hbox->addWidget(lw);
    hbox->addSpacing(5);
    hbox->addLayout(vbox);
    QWidget *wid = new QWidget;
    wid->setLayout(hbox);
    this->setCentralWidget(wid);
    ...............
    ..................
    //setLayout(hbox);

    }@



  • :o smart. :D i just have to practice some things more, i didnt get some parts right, thanks so much, i didnt get the Qframe right, dont know what it is and what it used for, tutorial had no explains ! also not sure what a QWidget And main window class is, i thought QWidget is the window that shows my widgets


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.