I am new to Qt. As it stands, I have a table with a button btn. When the button is clicked the setCentralWidget(view) takes over the window so I can no longer see the table obviously. But if I remove the setCentralWidget(view), nothing displays when I click the button.
Is there a way I can display both in the same window? Split or dock maybe?
(I have removed code that is irrelevant to my question)
MainWindow::MainWindow()
{
//etc
packet = new QTabWidget;
setCentralWidget(packet)
}
//other code
void MainWindow::create(const QString &a)
{
QTableWidget* table = new QTableWidget;
int tabIndex = packet->addTab(table, a);
packet->setCurrentIndex(tabIndex);
table->setRowCount(1);
table->setColumnCount(2);
table->setHorizontalHeaderLabels(QString("a;Simulator").split(";"));"));
table->setItem(0,0,new QTableWidgetItem(a));
QPushButton *btn = new QPushButton("load", this);
connect(btn, SIGNAL(clicked()), this, SLOT(sim()));
table->setCellWidget(0,1, btn);
}
void MainWindow::sim()
{
QGraphicsScene* scene = new QGraphicsScene(QRect(-10, -10, 100, 50));
QGraphicsView* view = new QGraphicsView();
scene->addText("Network");
view->setScene(scene);
view->setGeometry(QRect(10, 10, 100, 50));
setCentralWidget(view);
}
You should look at the QMdiArea class - it's designed to be used as the central widget of a Main Window that can have many inner widgets. It has a variety of layout styles as well - scroll down on that page to see the examples.
Hope that helps!
You should subclass QWidget. For example make your own MyWidget class. And this class will include a QGraphicsView and a QTabWidget in a splitter for example. And in your MainWindow set the central widget as an instance of MyWidget.
Related
I have two different views to display inside a QWidget window. Each view has a separate QGraphicsScene.
I want to toggle between the two views on button click.
This is my current implementation:
void toggleUi(bool type){
QGraphicsView* currentView;
if(bool){
currentView = getFirstView(); // returns QGraphicsView of first type
}
else{
currentView = getSecondView(); // returns QGraphicsView of second type
}
QLayout* layout = widget->layout ();
if (layout != 0)
{
QLayoutItem *item;
while ((item = layout->takeAt(0)) != 0)
layout->removeItem (item);
delete layout;
}
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(currentView);
}
Issue: Both views are displayed over one another on toggle, even after deleting the layout and adding a new one.
Both views are rendered fine without toggle.
Is there a better/another way to do it?
Use QStackedWidget! Use the addWidget function to add widgets:
QStackedWidget *stackedWidget = new QStackedWidget(this);
stackedWidget->addWidget(getFirstView());
stackedWidget->addWidget(getSecoundView());
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(stackedWidget);
setLayout(layout);
Here's how to switch between the widgets:
void toggleUi(bool type){
if(condition)
stackedWidget->setCurrentindex(0)
else
stackedWidget->setCurrentindex(1)
I hope it helps!
I have so when a person clicks a button it should duplicate a tab but I have run into the problem that tab->layout() only returns QLayout and I can't convert it QHBoxLayout.
void MainWindow::on_dublicateSection_clicked()
{
QWidget* tab = tabWidget->currentWidget();
QWidget* newTab = new QWidget(tab);
QHBoxLayout* layout = new QHBoxLayout(tab->layout());
newTab->setLayout(layout);
content->IncreaseArraySize(Section(tabWidget->count()));
QString tabText = tabWidget->tabText(tabWidget->currentIndex());
content->sections[tabWidget->count()].name = tabText;
tabWidget->addTab(newTab,tabText);
}
Actually you can.
QHBoxLayout* hbLayout = qobject_cast<QHBoxLayout*>(tab->layout());
Q_ASSERT(hbLayout);
But keep in mind that QObject derived classes are NOT copyable.
I have an MDI application, with classes as
class MainWindow
{ GraphicsView *gv; };
class GraphicsView
{ Scene *scene; };
class Scene
I'm creating a new mdiSubWindow on every newfile() of MainWindow which creates a new pointer to the GraphicsView.
void MainWindow::newFile()
{
gv = new GraphicsView;
QMdiSubWindow *w = mdiArea->addSubWindow(gv);
mdiArea->setActiveSubWindow(w);
}
And the constructor of GraphicsView creates a new Scene.
GraphicsView::GraphicsView()
{
scene = new Scene;
setScene(scene);
}
Now when there are multiple subWindows created, I lose the ability to work in previous subWindows. Only the latest subWindow works as expected. For eg. I can draw QGraphicsItems only in the latest Sub Windows and not in the previous ones.
I think I should be using activeSubWindow() but couldn't figure out how to make every subWindow respond to the change of the tabs. How should I implement this?
To make it work.
I've created a QList<QPair>, for storing a pair of subwindow and view.
windowViewList.append(qMakePair(w, view));
Then subWindowActivated() signal is used to call the following function to update the view pointer.
void MainWindow::updatePointers()
{
QMdiSubWindow *m = mdiArea->activeSubWindow();
foreach (windowViewPair v, windowViewList)
{
if (m == v.first)
gv = v.second;
}
}
I opened each Image in a new tab on QGraphicsScene and QGraphicsView by this
void MainWindow::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
QMessageBox::information(this, tr("Master Measure"),
tr("Cannot load %1.").arg(fileName));
return;
}
scene = new QGraphicsScene;
view = new QGraphicsView;
view->setScene(scene);
tabWidget->addTab(view,"someTab");
scene->addPixmap(QPixmap::fromImage(image));
scene->setBackgroundBrush(QBrush(Qt::lightGray, Qt::SolidPattern));
QFileInfo fileInfo = fileName;
tabWidget->setTabText(ui->tabWidget->count()-1, fileInfo.baseName());
tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
}
}
I want to draw something on each image by clicking.
so I did this by click press event
void MainWindow::mousePressEvent(QMouseEvent *event)
{
QPen pen(Qt::black);
QBrush brush(Qt::red);
pen.setWidth(6);
scene->addEllipse(0,0,1000,500,pen,brush);
}
it just draw Ellipse on last opened image (tab).
I don't know how to solve this problem.
I appreciate any idea.
Thank you.
Obviously scene variable points to the last created scene. When you create new scene, old pointer is lost because you don't save it anywhere. So you need to preserve all scene and view pointers somewhere and use the currently visible objects.
I advise you to create a QGraphicsView's subclass (let's call it MyView) that will be responsible to every tab's contents. Pass the filename to the constructor of this object. In the constructor create a scene and store it in a member variable. Reimplement MyView::mousePressEvent to perform drawing.
Then you can add new tabs like this:
MyView* view = new MyView(filename);
view ->addTab(view,"someTab");
When the user clicks a view, MyView::mousePressEvent method or the respective MyView object will be invoked. Each view will see only its own scene variable, and the respective scene will be edited.
I am adding three tables dynamically in widget containing table widget and labels, but nothing shows on screen, I have tried to do it with vertical layout but it does not expand if i add a new row, so not scrolling.
Is there any other way to get all three tables on a same page with scrolling.
QScrollArea *m_scrollArea =ui->scrollArea_Stats;
m_scrollArea->setWidgetResizable(true);
QWidget *area = new QWidget;
QVBoxLayout *vlay = new QVBoxLayout(m_scrollArea);
area->setLayout(vlay);
StatsWidget *objStatsWidget;
for(int i=0;i<2;i++)
{
objStatsWidget=new StatsWidget(ui->scrollArea_Stats);
vlay->addWidget(objStatsWidget);
}
m_scrollArea->setWidget(area);
here StatsWidget is my custom widget containing 2 lables at top and a table widget
I am adding three tables dynamically but page is not scrolling, vlay is not showing all tables it is just showing what it can show in a page without scrolling.
try rewrite the code as this:
m_scrollArea->setWidgetResizable(true);
QVBoxLayout *vlay = new QVBoxLayout;
StatsWidget *objStatsWidget;
for(int i=0;i<2;i++)
{
objStatsWidget=new StatsWidget(ui->scrollArea_Stats);
vlay->addWidget(objStatsWidget);
}
QWidget *area = new QWidget(m_scrollArea);
area->setLayout(vlay);
m_scrollArea->setWidget(area);
EDIT: i made something like what you are trying to do some time ago..
so: create a custom QWidget with a QVBoxLayout as member.let's call this object "widgetList". then reimplement all method that you need, as addWidget, takeAt etc.. using your layout as a list
finally set widgetList as widget for your scroll area..
let me know..
I made all this because QWidgetList was not enough easy to use and i needed something else that i have omitted here..
I found my piece of code:
class WidgetList : public QWidget
{
Q_OBJECT
public:
WidgetList(QWidget *parent = 0);
~WidgetList();
void addWidget(QWidget*);
void removeWidget(QWidget*);
QList<QWidget*> getListWidget() const;
QWidget* takeAt(int) const;
int count() const;
private:
QVBoxLayout* layout_;
};
.cpp
WidgetList::WidgetList(QWidget *parent)
: /**/QWidget(parent)
/**/,layout_(new QVBoxLayout(this))
{
this->setLayout(layout_);
}
void WidgetList::removeWidget(QWidget* widget)
{
layout_->removeWidget(widget);
}
void WidgetList::addWidget(QWidget* widget)
{
layout_->addWidget(widget);
}
QWidget* WidgetList::takeAt(int index) const
{
return layout_->takeAt(index)->widget();
}
int WidgetList::count() const
{
return layout_->count();
}
this will be your new Widget with layout where to insert your custom widget..
then i put widgetList as widget of QScrollArea:
QScrollArea* scrollArea = new QScrollArea;
widgetList* list = new widgetList(scrollArea);
scrollArea->setWidget(list);
everything works for me..
EDIT 2: i post my main that works good with my previous code:
QScrollArea* scroll = new QScrollArea;
WidgetList* w = new WidgetList(scroll);
QLabel * label = new QLabel("Label1");
QLabel* label2 = new QLabel("label2");
QTableWidget* table = new QTableWidget(10,10);
w->addWidget(label);
w->addWidget(label2);
w->addWidget(table);
scroll->setWidget(w);
scroll->setWidgetResizable(true);
scroll->show();