How to get QTabWidget title text of QWidget in Qt? - c++

I know I can use the widget function of QTabWidget in order to get the the QPlanTextEdit from the specified tab. But how can I get the tab title text of the current tab widget?
QPlainTextEdit* pTextEdit = NULL;
QWidget* pWidget= ui->tabWidget->widget(1);
if (pWidget->metaObject()->className() == "QPlainTextEdit")
pTextEdit = (QPlainTextEdit*)pWidget;
else
{
QList<QPlainTextEdit *> allTextEdits = pWidget->findChildren<QPlainTextEdit *>();
if (allTextEdits.count() != 1)
{
qError() << "Error";
return;
}
pTextEdit = allTextEdits[0];
}
ptextEdit->setPlainText("Updated Plain Text Edit);
// HERE I NEED THE CURRENT TAB'S TEXT!!

int index = ui->tabWidget->currentIndex();
QString currentTabText = ui->tabWidget->tabText(index);

Related

QMessage::question(),,the dialog should be modal but it's not somtimes,the dialog was showed under the MainWindow

//QMessageBox dialog is modal by default,but the dialog was showed under the mainwindow
int res = QMessageBox::question(NULL, tr("Question"), tr("Are you sure to delete this rules?"),
QMessageBox::Yes | QMessageBox::Cancel);
if (QMessageBox::Yes == res)
{
QStringList result;
for (int i = selectedrowset.size() - 1; i >= 0; i--)
{
//record data
QTableWidgetItem* item = this->item(selectedrowset.at(i), 0);
if (item)
{
QString text = item->text();
if (!result.contains(text))
result.append(text);
}
this->removeRow(selectedrowset.at(i));
}
emit signal_delete(result);
}
A QDialog is modal to its parent(s), but are giving no parent at all, since you're using NULL.
As shown in the documentation examples:
int ret = QMessageBox::warning(this, tr("My Application"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard
| QMessageBox::Cancel,
QMessageBox::Save);
Add the main window as a parent (the first argument), and the messagebox will be modal to it.

how to get row number after comboBox item in QTableWidget in qt

I would like to get a number of a QTableWidget row after selecting some topic in comboBox how it is possible to get the row, thanks.
void MainWindow::metto_stringa(int i)
{
QWidget *w = qobject_cast<QWidget *>(sender()->parent());
if(w)
{
int row = ui->tableWidget->indexAt(w->pos()).row();
ui->lineEdit->setText(QString::number( row ));
}
// ui->lineEdit->setText(QString::number( i ));
}
else if(i == 3)
{
// ui->tableWidget->setCellWidget(ui->tableWidget->rowCount(), i, "");
QString s = "Normal";
QComboBox *combo = new QComboBox;
combo->addItem("Below normal");
combo->addItem("Normal");
combo->addItem("Above normal");
combo->addItem("High");
combo->addItem("Real time");
connect(combo,SIGNAL(currentIndexChanged(int)),this,
SLOT(metto_stringa(int)));
ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1, i,combo);
/* ui->tableWidget->setCellWidget(i,4,combo);
QTableWidgetItem*item = new QTableWidgetItem(s);
item->setFlags(item->flags() ^ Qt::ItemIsEditable);
ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, i,
item);*/
continue;
}
In this case you should not use the parent of the QComboBox, you must use the same sender()
void MainWindow::metto_stringa(int index)
{
QWidget *w = qobject_cast<QWidget *>(sender());
if(w)
{
int row = ui->tableWidget->indexAt(w->pos()).row();
ui->lineEdit->setText(QString::number(row));
}
}
In the question I answered before I commented that you must access the widget that you use in the setCellWidget() function, in the previous case the widget had the following form:
QWidget <--- QPushButton
parent() sender()
ie you owe to that widget so we take advantage of sender() and parent() in the previous case. In the current case QComboBox is added directly.

Getting a widget by string

In a for loop, I can construct a string.
foreach (...) {
QString str = "pushButton" + QString::number(count);
// this gives pushButton1
// Now I want to get the button widget that has this string as it's variable name
// and modify it in some way, e.g. change it's button caption
str->setText("New Title");
count ++;
}
Is this possible? if so, then how
edited: here's how i created the buttons
for (int i=0; i<total; i++) {
QPushButton *btn = new QPushButton();
btn->setObjectName("pushButton" + QString::number(i));
ui->horizontalLayout->addWidget(btn);
}
You can get the button through the parent and objectName, in your case the parent is this, so you should use the following:
QWidget* p = ui->horizontalLayout->parentWidget();
for(int count=0; count<total; count++){
const QString str = "pushButton" + QString::number(count);
QPushButton* btn = p->findChild<QPushButton *>(str);
btn->setText("someText");
}

how to make ticker in qgraphicsrectitem in Qt

how to make ticker in qgraphicsrectitem in Qt
i tried following
rect = new QGraphicsRectItem;
text = new QGraphicsTextItem;
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
rect = scene->addRect(0,0,500,500);
text->setPlainText("HELLO from constructor");
scene->addItem(text);
QFont font;
fm = new QFontMetrics(font);
ntextWidth = fm->width(text->toPlainText());
text->setPos(0,rect->boundingRect().height()/2);
ntextHeight = fm->height();
QString strtxt = fm->elidedText(text->toPlainText(), Qt::ElideRight, ntextWidth-50, Qt::TextShowMnemonic);
qDebug() << "Here Lenght in pixels:" << ntextWidth <<"====" <<ntextHeight <<"-----"<<strtxt.contains("…");
connect(&timer, SIGNAL(timeout()),this,SLOT(update()));
timer.start(5);
}
GraphicsTicker::~GraphicsTicker()
{
delete ui;
}
void GraphicsTicker::update()
{
text->setPos(text->x()+1,text->y());
if(text->x()==rect->boundingRect().width()-ntextWidth)
{
ntextWidth = ntextWidth-1;
strNewText = fm->elidedText(text->toPlainText(),Qt::ElideRight,ntextWidth).remove("…");
text->setPlainText(strNewText);
}
if(text->x() == rect->boundingRect().width()-20)
{
text->setPlainText("HELLO from constructor");
ntextWidth = fm->width(text->toPlainText());
text->setPos(0,text->y());
}
I have used qgraphicstextitem and qgraphicsrectitem for this.
I moved the text depends on the position of rect using timer.
But it is not as perfect as Ticker
Please give me smart solution
Thanks in Advance
I have done with ticker in Qt.
I have simply used Qlable and using this I rotated text on it and then I added this lable in Qgraphicsscene with Qgraphicsproxywidget.
Thank you all.
you can make ticker by using Qpropertyanimation by simply adding Qlable into widget and set its start and end value so that it can look like a ticker is moving. For this "geometry" of label is consider for moving

Qt Localization issue when using QAction and setToolTip()

I am trying to use a Turkish translation file for my Qt project. I used Qt Linguist for generating the .ts file. The problem is when I load the translation file in my application, I am getting segmentation faults whenever I click on any item of QMenuBar.
I also have a context menu that is triggered with the contextMenuEvent of a GraphicsView in mainwindow. The program gives a segmentation fault when I try to execute the below line.
mContextMenu->exec(event->globalPos());
mContextMenu is a QMenu* and exec returns a QAction*. Basically I guess that when the translation belongs to a QAction this problem occurs. I saw the same problem when I translate the toolTips of toolButtons. How should I handle the translation of QActions and toolTips?
I am adding some actions to mContextMenu as below:
void RadarView::prepareMainMenu() {
mContextMenu = new QMenu();
//showLineAction->setShortcut(QKeySequence("Alt+Shift+L"));
mpStartRulerAction = new QAction(QObject::tr("Start Ruler"), this);
mContextMenu->addAction(mpStartRulerAction);
connect(mpStartRulerAction, SIGNAL(triggered()), this,
SLOT(menuStartRulerClicked()));
mpStartRulerAction->setProperty("TYPEVIEW", MV_StartRuler);
mpEndRulerAction = new QAction(QObject::tr("End Ruler"), this);
mContextMenu->addAction(mpEndRulerAction);
connect(mpEndRulerAction, SIGNAL(triggered()), this, SLOT(menuEndRulerClicked()));
mpEndRulerAction->setProperty("TYPEVIEW", MV_EndRuler);
mpCriticalRegionAction = new QAction(QObject::tr("Critical Region"), this);
mContextMenu->addAction(mpCriticalRegionAction);
connect(mpCriticalRegionAction, SIGNAL(triggered()), this, SLOT(menuDefineCriticalRegionClicked()));
mpCriticalRegionAction->setProperty("TYPEVIEW", MV_Critical_Region);
mpAScopeAction = new QAction(QObject::tr("A-Scope Line"), this);
mContextMenu->addAction(mpAScopeAction);
connect(mpAScopeAction, SIGNAL(triggered()), this, SLOT(menuAddAScopeLine()));
mpAScopeAction->setProperty("TYPEVIEW", MV_A_Scope);
}
Context Menu event of graphics view is implemented as below:
void RadarView::contextMenuEvent(QContextMenuEvent * event) {
LOGGER_START
//check if the item has its own context menu...
QList<QGraphicsItem*> items = this->items(event->pos());
if (items.size() != 0) {
bool isValid = true;
for (int i = 0; i < items.size(); ++i) {
QGraphicsRulerLineItem *rulerLineItem = NULL;
rulerLineItem = dynamic_cast<QGraphicsRulerLineItem*> (items[i]);
if (rulerLineItem != NULL || dynamic_cast<AScopeLineItem*> (items[i]) || dynamic_cast<PpiTargetItem*> (items[i])){
isValid = false;
}
else {
PpiPlotItem *targetItem = NULL;
targetItem = dynamic_cast<PpiPlotItem*> (items[i]);
if (targetItem != NULL) {
isValid = false;
if (currentVisibleMenuItems.contains(0)) {
startItem = targetItem;
} else if (currentVisibleMenuItems.contains(1)) {
endItem = targetItem;
}
}
}
}
if (isValid == true) {
if(currentVisibleMenuItems.size() ==0)
return;
//Get last clicked Position.
mLastClickedPos = event->pos();
//show menu of RadarView...
mpRightClickAction = mContextMenu->exec(event->globalPos());
} else {
QGraphicsView::contextMenuEvent(event);
}
}
LOGGER_END
}
The problem I described above is solved when I used QApplication instead of a customized class that inherits QApplication. Inside of the customized class, bool QApplication::notify ( QObject * receiver, QEvent * e ) function was implemented. When I used QApplication directly, all translations were loaded correctly and no problems ocuured regarding the QAction items and toolTips.