Delete created Qt elements and subclasses - c++

I have a question about Qt. I am wondering how it is I should delete all the pointers I create. For example:
.h file
#ifndef MAINCALENDAR_H
#define MAINCALENDAR_H
#include<QWidget>
#include <QMap>
#include <QComboBox>
#include <QCalendarWidget>
#include <QRadioButton>
#include <QString>
#include <QtGui>
#include "selector.h"
#include <QInputDialog>
class mainCalendar : public QWidget
{
Q_OBJECT
public:
mainCalendar(QWidget * parent = 0);
~mainCalendar();
void showAppointments();
public slots:
void showLCFunc() {select->getTod()->getIntf()->getListClass().orderListChronologic(); printer * test = new printer; test->setList(TodFace->getList()); test->show();}
void showLPFunc() {select->getTod()->getIntf()->getListClass().orderListByPriority(); printer * test = new printer; test->setList(TodFace->getList()); test->show();}
void loadFile() {QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"", tr("M-Calendar Files (*.mca)"));}
void saveFile() {QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"", tr("M-Calendar Files (*.mca)"));qDebug() << fileName << endl;}
void intshow();
void updater() {if (!Interface->isHidden()) { app->setList(Interface->getList()); app->Beta_update(1,calendar->selectedDate()); }
else if (!TodFace->isHidden()) {tod->setList(TodFace->getList()); tod->Beta_update(1,calendar->selectedDate());} }
private:
QPushButton *showLC;
QPushButton *showLP;
QPushButton *searchButton;
QPushButton *updateButton;
QPushButton *saveButton;
QPushButton *loadButton;
QLabel *instructions;
QPushButton *backButton;
QPushButton *taskButton;
interface * Interface;
todFace * TodFace;
showTod * tod;
showApp * app;
QCalendarWidget *calendar;
QGridLayout *mainLayout;
Selector * select;
};
#endif // MAINCALENDAR_H
.cpp file:
#include "maincalendar.h"
mainCalendar::mainCalendar(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout * footButtons = new QHBoxLayout;
showLC = new QPushButton(tr("'to-do' (chrono)"));
showLP = new QPushButton(tr("'to-do' (priority)"));
searchButton = new QPushButton(tr("&Search"));
saveButton = new QPushButton(tr("&Save calendar"));
loadButton = new QPushButton(tr("&Load Calendar"));
updateButton = new QPushButton(tr("Update"));
footButtons->addWidget(searchButton);
footButtons->addWidget(saveButton);
footButtons->addWidget(loadButton);
footButtons->addWidget(showLC);
footButtons->addWidget(showLP);
instructions = new QLabel(tr("To view or add data, double-click date in calendar"));
calendar = new QCalendarWidget;
calendar->setGridVisible(true);
calendar->setMinimumDate(QDate(2012, 1, 1));
calendar->setMaximumDate(QDate(2016,12,31));
backButton = new QPushButton(tr("&Back to calendar"));
select = new Selector(0,calendar,instructions,backButton, updateButton);
tod = select->getTod();
TodFace = tod->getIntf();
TodFace->hide();
TodFace->setCalendar(calendar);
tod->hide();
app = select->getApp();
Interface = app->getIntf();
Interface->hide();
Interface->setCalendar(calendar);
app->hide();
backButton->hide();
updateButton->hide();
connect(showLC,SIGNAL(clicked()),this,SLOT(showLCFunc()));
connect(showLP,SIGNAL(clicked()),this,SLOT(showLPFunc()));
connect(updateButton, SIGNAL(clicked()), this, SLOT(updater()));
connect(backButton, SIGNAL(clicked()), this, SLOT(intshow()));
connect(loadButton,SIGNAL(clicked()),this,SLOT(loadFile()));
connect(saveButton,SIGNAL(clicked()),this,SLOT(saveFile()));
connect(calendar, SIGNAL(activated(QDate)), this, SLOT(intshow()));
mainLayout = new QGridLayout;
this->setMinimumHeight(800);
this->setMinimumWidth(1000);
mainLayout->setColumnMinimumWidth(0,500);
mainLayout->addWidget(calendar,0,0);
mainLayout->addWidget(app,1,0);
mainLayout->addWidget(Interface,1,2);
mainLayout->addWidget(tod,1,0);
mainLayout->addWidget(TodFace,1,2);
mainLayout->addWidget(backButton,0,0,Qt::AlignTop);
mainLayout->addLayout(footButtons,2,0,Qt::AlignLeading);
mainLayout->addWidget(instructions,2,0,Qt::AlignTrailing);
mainLayout->addWidget(updateButton,2,2,Qt::AlignRight);
setLayout(mainLayout);
setWindowTitle(tr("M-Calendar"));
}
mainCalendar::~mainCalendar()
{
}
void mainCalendar::intshow()
{
if (Interface->isHidden()&&TodFace->isHidden())
{
select->setDate(calendar->selectedDate());
select->show();
Interface->setdate(calendar->selectedDate());
TodFace->setdate(calendar->selectedDate());
} else
{
backButton->hide();
updateButton->hide();
Interface->hide();
app->close();
TodFace->hide();
tod->close();
calendar->show();
instructions->show();
}
}
I am stuck here. Am I supposed to do to delete all the pointers (QPushbutton, etc) and subclasses so no memory leaks occur?

Short answer: No, you don't have to explicitly delete them.
In Qt, the QObjects are organized in object trees. The parent-child relationships of the various widgets also implies that the parent takes ownership of the child widgets.
As a result you do not have to explicitly delete them when your application finishes. Each parent will take care of the cleanup of its own children. Only when you create a (pointer to) a widget/object which has no parent, you will need to explicitly delete it.

via addWidget you create a parent-child-relationship between these to widgets.
Any parentless widget has to be destroyed manually, which in turn free its children recursivly.
As long as your widget has a parent, and that is either freed manually or child to another Widget, no manual free is required.
Same accounts for QObject derived classes.

Related

Accessing the functions of custom QWidget class using Signals and Slots

I have a MediaView class which inherits QWidget and has GUI components. And i have another class MediaController which handles the changes made on the GUI components.
I need:
to enable buttons when i receive the signal finishedLoadingFile()
So here is a sample how my class looks:
MediaView.cpp
MediaView::MediaView(QWidget* parent) : QWidget(parent)
{
m_controller = new MediaController(this);
m_btnPlay = new QPushButton(m_mediaButtons);
m_btnPlay->setIcon(QIcon(":/images/play.png"));
m_btnPlay->setIconSize(QSize(32, 32));
m_btnPlay->move(100, 10);
m_btnPlay->resize(39, 38);
m_btnPlay->setToolTip("Play");
m_btnPause = new QPushButton(m_mediaButtons);
m_btnPause->setIcon(QIcon(":/images/pause.png"));
m_btnPause->setIconSize(QSize(32, 32));
m_btnPause->move(150, 10);
m_btnPause->resize(39, 38);
m_btnPause->setToolTip("Pause");
m_btnPlay->setEnabled(false);
m_btnPause->setEnabled(false);
connect(m_btnOpen, SIGNAL(clicked(bool)), m_controller, SLOT(loadFile()));
connect(m_btnPlay,SIGNAL(clicked(bool)),m_controller,SLOT(buttonPlayClicked()));
connect(m_btnPause,SIGNAL(clicked(bool)),m_controller,SLOT(buttonPauseClicked()));
}
void MediaView::enableButtons()
{
m_btnPlay->setEnabled(true);
m_btnPause->setEnabled(true);
}
MediaController.cpp
MediaController::MediaController(QWidget* parent) : QWidget(parent)
{
MediaView* view = new MediaView(this); //loop here
connect(this, SIGNAL(loadingFileFinished(), view, SLOT(enableButtons()));
}
void MediaController::loadFile()
{
m_filePath = "";
m_filePath = QFileDialog::getOpenFileName(this, "Choose a .txt file with paths to gtt streams", QString(),
QString("text file(*.txt)"));
if (m_filePath.isEmpty())
{
return;
}
m_file = "";
m_file.append(m_filePath.toLocal8Bit().constData());
emit finishedLoadingFile();
}
Issue:
I wish to call the enableButtons() in the MediaView class. But when i instantiate the MediaView class inside MediaController, app crashes because they keep calling each others constructors in a loop. Reason being, they both communicate using signals and slots so i need to instantiate objects of their respective classes.
Alternatives:
I would like to know whether if it is possible to access the widgets from MediaController class so that i can use them there itself?
If not, a better way to communicate between view and controller?
Each MediaView creates a MediaController, and each MediaController creates another MediaView, that generates the infinite loop, the appropriate thing is to create a MediaController in MediaView and make the connections:
MediaView::MediaView(QWidget* parent) : QWidget(parent)
{
m_controller = new MediaController(this);
connect(m_controller, &MediaController::loadingFileFinished, this, &MediaView::enableButtons);
[...]
}
it is not necessary to create a new MediaView within MediaController.
If you want to access MediaView from MediaController you can do it through parent since MediaView is the parent of MediaController:
MediaController.h
MediaView *view;
MediaController.cpp
MediaController::MediaController(QWidget* parent) : QWidget(parent)
{
view = qobject_cast<QMediaView *>(parent);
//or
// connect(this, &MediaController::loadingFileFinished, view, &MediaView::enableButtons);
}

Qt: Widget method addButtons() does NOT work as needed [duplicate]

This question already has answers here:
Unable to delete the widgets in sub-layout of a layout in Qt
(3 answers)
Closed 6 years ago.
I am using Qt5 (beginner) on Windows7.
In the main window of my app I want to display and remove some push-buttons.
widget = new ButtonWidget(ui->frame); // frame is a QScrollArea
connect(ui->addBtns, SIGNAL(clicked()), widget, SLOT(addButtons()));
connect(ui->deleteBtns, SIGNAL(clicked()), widget, SLOT(deleteButtons()));
And the ButtonWidget class is here:
ButtonWidget::ButtonWidget(QWidget * parent) : QWidget(parent)
{
//addButtons();
}
void ButtonWidget::addButtons()
{
QStringList texts{"1\nok", "2\nok", "3\nok", "4\nok", "5\nok", "6\nok"};
gridLayout = new QGridLayout;
for(int i = 0; i < texts.size(); i++)
{
QPushButton * button = new QPushButton(texts[i]);
gridLayout->addWidget(button, i / 5, i % 5);
}
setLayout(gridLayout);
}
// I'm not sure this method/function is ok... :(
void ButtonWidget::deleteButtons()
{
QLayoutItem * child;
while((child = gridLayout->takeAt(0)) != 0)
{
gridLayout->removeWidget(child->widget());
delete child->widget();
delete child;
}
delete gridLayout;
}
Problem is: when I click on add_buttons, I get all buttons displayed, but they are shrunk, tiny or something... :
OTOH... if I remove the comment from addButtons() call in the constructor (hence calling from within the constructor), the result is ok:
So, finally I have 2 questions:
1) How to fix the code to be able to add those buttons properly (when add_buttons is clicked)?
2) Is the deleteButtons() method ok?
First, I would recommend to do
gridLayout = new QGridLayout(this);
in the constructor. You don't need to delete all the grid, create it again and set in as a layout when you can just remove its content (with delete button for example) and fill it again afterwards.
EDIT : comment of Werner Erasmus : No need to set the layout. The fact that it has a parent widget implies that it sets itself up.
The problem is that is you do not modify addButtons() you will substitute the previous buttons without knowing where they go.
Also, try to give the QPushButton a parent :
new QPushButton(texts[i],this);
For your second point : Removing widgets from QGridLayout
EDIT:
After some more testing (without looking at the source code, but suspecting that it must be safe to delete button, else things would be brittle), I've implemented removeButtons as follows:
void ButtonWidget::deleteButtons()
{
while(myLayout->count())
{
delete myLayout->itemAt(0)->widget();
}
}
This works, and it proves that deleting a widget also removes the widget from it's parent, and layouts associated with the parent (by slots hooked up to when a child widget is deleted). The above code confirms this, as count(), which refers to number of layout items, decrease to zero (and those layout items are managed by the Layout). takeAt(x) is never called, and doesn't need to be - simply delete the widgets (buttons). Wholla!
ORIGINAL ANSWER
As mentioned in my other post, you only need to delete the buttons (it is removed from its parent automatically). However, if a widget was added to a layout, the parent of that layout becomes the parent of the widget, and an associated QLayoutItem is created that is managed by the layout itself. To the delete the buttons, the safest way is to take all the layout items (ownership the taker's responsibility), delete each items associated widget, and the delete each item. I'll try and find relevant references apart from the sources...
The following code works:
//ButtonWidget.h
#include <QWidget>
#include <QScrollArea>
#include <QHBoxLayout>
class ButtonWidget : public QScrollArea
{
Q_OBJECT
public:
ButtonWidget(QWidget *parent = 0);
~ButtonWidget();
void addButtons();
void deleteButtons();
private:
QHBoxLayout* myLayout;
};
//ButtonWidget.cpp
#include "ButtonWidget.h"
#include <QGridLayout>
#include <QPushButton>
#include <QLayoutItem>
ButtonWidget::ButtonWidget(QWidget * parent) :
QScrollArea(parent),
myLayout(new QHBoxLayout(this))
{
}
ButtonWidget::~ButtonWidget()
{
}
void ButtonWidget::addButtons()
{
QStringList texts{"1\nok", "2\nok", "3\nok", "4\nok", "5\nok", "6\nok"};
for(int i = 0; i < texts.size(); i++)
{
myLayout->addWidget(new QPushButton(texts[i]));
}
}
void ButtonWidget::deleteButtons()
{
QLayoutItem * child;
while((child = myLayout->takeAt(0)) != 0)
{
delete child->widget();
delete child;
}
}
#include "ButtonWidget.h"
#include <QApplication>
#include <QScrollArea>
#include <QPushButton>
#include <QGridLayout>
#include <QHBoxLayout>
#include <memory>
std::unique_ptr<QScrollArea> makeArea()
{
std::unique_ptr<QScrollArea> area(new QScrollArea);
auto layout = new QGridLayout(area.get());
auto addButton = new QPushButton("Add");
auto removeButton = new QPushButton("Remove");
layout->addWidget(addButton, 0, 0);
layout->addWidget(removeButton, 0, 1);
auto btnWidget = new ButtonWidget;
layout->addWidget(btnWidget,1,0,1,2);
QObject::connect(addButton, &QPushButton::clicked, [=]()
{
btnWidget->addButtons();
});
QObject::connect(removeButton, &QPushButton::clicked, [=]()
{
btnWidget->deleteButtons();
});
return move(area);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto area = makeArea();
area->show();
return a.exec();
}
You need to enable c++11 in your config (.pro) to get the lambdas working.
CONFIG += c++11
I've used QHBoxLayout for your buttons, as it better models what you want. Although strictly not necessary, I'm returning unique_ptr from the makeArea in main, as it has not parent, I'm not sure whether it gets some parent because it is the first widget created, but unique_ptr shows intent.
NOTE:
Apparently the layout item is not the parent of the widget, but the widget associated with the layout itself is the parent of widgets belonging to its layout item.

Qt - Adding data to a TableWidget via another TableWidget

I am stuck on a certain problem with table widgets and I am in need of help. Basically, I am trying to get a table widget on a dialogue screen to get the information inside of it and then fill that information on to another table widget on a main window screen. So, when I click the OK button, it should take the text from one table widget and place it in the other table widget. I tried using the below code, and the build ran. But, as soon as I clicked OK, the program crashed. The table called TableWidgetedit is the table I am trying to copy from and send to the table in the mainwindow, named tablewidget. (just to make so I am not vague, I am trying to copy data from the one table and place it in another table, when a user clicks on the OK button.)
int rows = 6;
int columns = 5;
Ui::MainWindow *mainui;
void EditMode1::on_pushButton_clicked()
{
for (int i = 0; i<columns;++i){
for (int j = 0;j<rows;++j){
QTableWidgetItem *celltxt= TableWidgetedit.item(j,i);
mainui->tableWidget->setItem(j,i,celltxt);
}
}
}
Any and all help is appreciated. Thank you!
(I am a not the best with Qt so if you don't mind explaining what changes you have made and why, that would a great help thank you!).
-UPDATE-
#Jeet here are the code for what im trying to do:
tablemainwindow1.h:
#ifndef TABLEMAINWINDOW1_H
#define TABLEMAINWINDOW1_H
#include <QMainWindow>
#include "tabledialougewindow.h"
namespace Ui {
class TableMainWindow1;
}
class TableMainWindow1 : public QMainWindow
{
Q_OBJECT
public:
explicit TableMainWindow1(QWidget *parent = 0);
~TableMainWindow1();
private slots:
void on_pushButton_clicked();
private:
Ui::TableMainWindow1 *ui;
TableDialougeWindow *tbl2;
};
#endif // TABLEMAINWINDOW1_H
tablemainwindow1.cpp:
#include "tablemainwindow1.h"
#include "ui_tablemainwindow1.h"
TableMainWindow1::TableMainWindow1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TableMainWindow1)
{
ui->setupUi(this);
ui->tableWidget->setRowCount(3);
ui->tableWidget->setColumnCount(3);
}
TableMainWindow1::~TableMainWindow1()
{
delete ui;
}
void TableMainWindow1::on_pushButton_clicked()
{
tbl2 = new TableDialougeWindow(this);
tbl2->show();
}
tabledialougewindow.h:
#ifndef TABLEDIALOUGEWINDOW_H
#define TABLEDIALOUGEWINDOW_H
#include <QDialog>
namespace Ui {
class TableDialougeWindow;
}
class TableDialougeWindow : public QDialog
{
Q_OBJECT
public:
explicit TableDialougeWindow(QWidget *parent = 0);
~TableDialougeWindow();
private slots:
void on_buttonBox_accepted();
private:
Ui::TableDialougeWindow *ui;
};
#endif // TABLEDIALOUGEWINDOW_H
tabledialougewindow.cpp:
#include "tabledialougewindow.h"
#include "ui_tabledialougewindow.h"
#include "tablemainwindow1.h"
#include "ui_tablemainwindow1.h"
int Rows = 3;
int Columns = 3;
Ui::TableMainWindow1 *mainui;
TableDialougeWindow::TableDialougeWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TableDialougeWindow)
{
ui->setupUi(this);
ui->tableWidget->setRowCount(Rows);
ui->tableWidget->setColumnCount(Columns);
}
TableDialougeWindow::~TableDialougeWindow()
{
delete ui;
}
void TableDialougeWindow::on_buttonBox_accepted()
{
for(int i = 0;i<Columns;++i){
for(int j = 0;j<Rows;++j){
QTableWidgetItem *celltxt = ui->tableWidget->item(j,i);
QTableWidgetItem *celltxt2 =new QTableWidgetItem(*celltxt);
mainui->tableWidget->setItem(j,i,celltxt2);
}
}
accept();
}
hope this helps.
The error is we cannot insert an item that is already owned by another QTableWidget. So before make a copy we need to clone the data. we can use copy constructor to achieve this. Code comment will explain in detail. Hope this help.
void MainWindow::on_cmdTransfer_clicked()
{
//Source Table
int rows = ui->tbl1->rowCount();
int columns =ui->tbl1->columnCount();
//Destination Table
ui->tbl2->setColumnCount(columns);
ui->tbl2->setRowCount(rows);
//Copy data form one table to another.
for (int i = 0; i<columns;++i){
for (int j = 0;j<rows;++j){
QTableWidgetItem *celltxt= ui->tbl1->item(j,i);
//Clone the data using copy constructor
QTableWidgetItem *celltxt1=new QTableWidgetItem(*celltxt);
ui->tbl2->setItem(j,i,celltxt1);
}
}
}

QTableView - Place pointer(highlight selection to the first row of list

I create own widget based on QTableView. It's something like file dialog (list). I want to act intuitively.
a) working with whole rows
b) indicator also worked with whole rows
c) using switched enter the lower level (subdirectory)
d) after run program or moving to a lower level cursor must be on the first row of the table (row 0)
And there is problem. I can not force the program to place the cursor on the first line.
I tried several methods, but none succeeded. setCurrentIndex. selectRow etc. Cursor is always somewhere else. Not highlighted, not on first line, but once it's on 10 position second on 4 position etc. It behaves unpredictably.
How can I do it?
Here my code is:
mFileBoxWidget::mFileBoxWidget(QWidget *parent) :
QTableView(parent)
,model(new QFileSystemModel())
{
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setShowGrid(false);
this->verticalHeader()->setVisible(false);
this->installEventFilter(this);
model->setReadOnly(true);
this->setModel(model);
this->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch );
this->setColumnWidth(1,70);
this->setColumnWidth(2,70);
this->setColumnWidth(3,110);
this->setRootIndex(model->setRootPath("C://"));
this->setSelectionMode(QAbstractItemView::SingleSelection);
this->setSelectionBehavior(QAbstractItemView::SelectRows);
//this->selectRow(0); //Does not work - goto first row
//this->setCurrentIndes(Index); //Does not work - goto index x=0 y=0
}
Thank you with advance for all your responses.
Solved!
The problem is that the model is asynchronous. So reads the data in another thread. When I tried to set the index to the first line, still basically did not exist. The solution is, of course, to wait for loading the thread. At this point signal directoryLoaded(QString) is send. As a result, it is necessary to wait for the signal, and only then set index.
connect(myModel, SIGNAL(directoryLoaded(QString)), this, SLOT(onLoaded()));
void mFileBoxWidget::onLoaded()
{
QModelIndex index = myModel->index(myModel->rootPath());
this->setCurrentIndex(index.child(0, index.column()));
}
You shouldn't name your member variable model. QTableView has function model(), the compiler thinks this->model is meant to be this->model(), therefore you get the error you mentioned.
This is untested code, but I think something like this should work:
QModelIndex firstRow = QTableView::model()->index(0, 0);
QTableView::selectionModel()->select(firstRow,
QItemSelectionModel::ClearAndSelect |
QItemSelectionModel::Rows );
EDIT: (2013-06-19 06:12:58 UTC)
A simple (and ugly) workaround that worked so far for me is triggering a call to m_tableView->selectRow(0); from a QTimer.
Here's the sample code:
Header:
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
class QTableView;
class QFileSystemModel;
class MainWidget : public QWidget
{
Q_OBJECT
public:
MainWidget(QWidget *parent = 0);
~MainWidget();
private:
void layoutWidgets();
QFileSystemModel *m_model;
QTableView *m_tableView;
private slots:
void selectFirstRow();
// for debugging only
void selectionChanged();
};
#endif // MAINWIDGET_H
Implementation:
#include "mainwidget.h"
#include <QTableView>
#include <QHBoxLayout>
#include <QFileSystemModel>
#include <QHeaderView>
#include <QTimer>
MainWidget::MainWidget(QWidget *parent)
: QWidget(parent)
{
m_tableView = new QTableView(this);
m_model = new QFileSystemModel(this);
m_model->setReadOnly(true);
m_tableView->setModel(m_model);
m_tableView->setRootIndex(m_model->setRootPath(QDir::homePath()));
m_tableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_tableView->setShowGrid(false);
m_tableView->verticalHeader()->setVisible(false);
m_tableView->setColumnWidth(1,70);
m_tableView->setColumnWidth(2,70);
m_tableView-> setColumnWidth(3,110);
m_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
//m_tableView->->setSectionResizeMode(0, QHeaderView::Stretch ); // Qt 5?
layoutWidgets();
connect(m_tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged()) );
// This works
QTimer::singleShot(1000, this, SLOT(selectFirstRow()));
// Direct invocation - doesn't works
// selectFirstRow();
}
void MainWidget::layoutWidgets()
{
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(m_tableView);
setLayout(mainLayout);
setFixedSize(500,500);
}
void MainWidget::selectFirstRow()
{
m_tableView->selectRow(0);
}
void MainWidget::selectionChanged()
{
qDebug("Selection changed");
}
MainWidget::~MainWidget()
{}
The weird thing is, if QTimer::singleShot() needs to be triggered with a delay of at least ~25 ms., otherwise it wouldn't work in my system.
Here's the alternative, subclassing QTableView:
#include "mytableview.h"
#include <QFileSystemModel>
#include <QHeaderView>
#include <QTimer>
MyTableView::MyTableView(QWidget *parent) : QTableView(parent)
{
QFileSystemModel *myModel = new QFileSystemModel;
setModel(myModel);
setRootIndex(myModel->setRootPath(QDir::homePath()));
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setShowGrid(false);
verticalHeader()->setVisible(false);
//installEventFilter(this);
myModel->setReadOnly(true);
//setSectionResizeMode(0, QHeaderView::Stretch ); // Qt 5
setColumnWidth(1,70);
setColumnWidth(2,70);
setColumnWidth(3,110);
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
QTimer::singleShot(100, this, SLOT(selectFirstRow()));
connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged()));
}
void MyTableView::selectFirstRow()
{
// qDebug("Selecting first row");
// QModelIndex firstRow = QTableView::model()->index(0,0);
// if(firstRow.isValid()){
// selectionModel()->select(firstRow, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
// }else{
// qDebug("Invalid index");
// }
selectRow(0);
}
void MyTableView::selectionChanged()
{
qDebug("Selection changed.");
}

how to connect a QRadioButton to a QTabWidget?

i am creating a software using C++ and QT,and i have two widget one of type QRadioButon and one QTabWidget. My need is that i want to send signal from a radio button and i want that whenever the button is checked the content of tab get changed.
Can anyone suggest how to do that? I tried creating a slot of my widget class and in that slot i called the constructor of tab class but the problem is the construtor is not getting called.
here is the code i am using..
#include <QtGui>
#include "v_lab.h"
v_lab::v_lab(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("Virtual Lab");
maingroup=new QGroupBox(this);
maingroup->setTitle("Algorithms");
maingroup->setMinimumWidth(200);
maingroup->setMaximumWidth(240);
maingroup->setFlat(false);
p=new QPalette;
p->setColor(QPalette::Background,QColor(233,212,102));
setPalette(*p);
box=new QGroupBox(maingroup);
box->setFlat(false);
box->setTitle("Searching Algorithm");
linear_search=new QRadioButton("Linear Search",box);
linear_search->setChecked(1);
binary_search=new QRadioButton("Binary Search",box);
box1=new QGroupBox(maingroup);
box1->setFlat(false);
box1->setTitle("Sorting Algorithms");
bubble_sort=new QRadioButton("Bubble Sort",box1);
selection_sort=new QRadioButton("Selection Sort",box1);
box2=new QGroupBox(maingroup);
box2->setFlat(false);
box2->setTitle("Tree Algorithms");
infix_traversal=new QRadioButton("Infix Traversal",box2);
prefix_traversal=new QRadioButton("Prefix Traversal",box2);
postfix_traversal=new QRadioButton("Postfix Traversal",box2);
box3=new QGroupBox(maingroup);
box3->setFlat(false);
box3->setTitle("Graph Algorithms");
bfs=new QRadioButton("BFS",box3);
dfs=new QRadioButton("DFS",box3);
shortest_path=new QRadioButton("Shortest Path",box3);
QString string1="go to hell";
tab=new QTabWidget;
tab->addTab(new algorithm(string1),"Algorithm");
// tab->addTab(new psudo_code(),"Pseduo-Code");
tab->setMinimumWidth(250);
tab->setMaximumWidth(400);
//Layout
mainlayout=new QHBoxLayout(this);
mainlayout->addWidget(maingroup);
mainlayout->addWidget(tab);
mainlayout->addStretch();
main_left_pane_layout=new QVBoxLayout(maingroup);
main_left_pane_layout->addWidget(box);
main_left_pane_layout->addWidget(box1);
main_left_pane_layout->addWidget(box2);
main_left_pane_layout->addWidget(box3);
left_pane_box=new QVBoxLayout(box);
left_pane_box->addWidget(linear_search);
left_pane_box->addWidget(binary_search);
left_pane_box1=new QVBoxLayout(box1);
left_pane_box1->addWidget(bubble_sort);
left_pane_box1->addWidget(selection_sort);
left_pane_box2=new QVBoxLayout(box2);
left_pane_box2->addWidget(infix_traversal);
left_pane_box2->addWidget(prefix_traversal);
left_pane_box2->addWidget(postfix_traversal);
left_pane_box3=new QVBoxLayout(box3);
left_pane_box3->addWidget(bfs);
left_pane_box3->addWidget(dfs);
left_pane_box3->addWidget(shortest_path);
connect(binary_search,SIGNAL(clicked()),this,SLOT(peeyush()));
}
algorithm::algorithm(const QString &string,QWidget *parent)
: QWidget(parent)
{
label=new QLabel(string);
main_layout=new QVBoxLayout;
main_layout->addWidget(label);
main_layout->addStretch();
setLayout(main_layout);
}
/*
psudo_code::psudo_code(QWidget *parent)
: QWidget(parent)
{
label1=new QLabel("Hello Peeyush Chandel");
main_layout1=new QVBoxLayout;
main_layout1->addWidget(label1);
main_layout1->addStretch();
setLayout(main_layout1);
}
*/
void v_lab::peeyush()
{
QString string1="new string";
algorithm obj(string1);
//exit(1);
}
In the header definition file of your v_lab class you should have something like this:
// Includes here.
class v_lab: public QDialog
{
Q_OBJECT // VERY important!
public:
// Other things here.
private slots: // VERY important. You can use public slots too.
void peeyush();
}
And you cannot connect a signal to a constructor.