View Full Version : setAlignment does not work again
giusepped
27th May 2008, 09:12
I am trying to align the text inside a QTextEdit.
At bound the action menu to the setAlignment() slot of QTextEdit.
But at runtime, besides the fact that no alignment takes place, I got the errors:
Object::connect: No such slot QTextEdit::setAlignment(Qt::AlignRight)
and so on.
The signla/slot binding is:
connect(act_alignright, SIGNAL(triggered(bool)), textEdit,SLOT(setAlignment(Qt::AlignRight)));
Any reply will be very appreciated
mazurekwrc
27th May 2008, 09:39
first of all it won't work because
The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot.
so it's the reason why you get error
http://doc.trolltech.com/4.4/signalsandslots.html
giusepped
28th May 2008, 02:31
Don't understan:
the slot setAlignement is a slot of textEdit and act_alignright is a signal of a menu.
What am I missing?
G
giusepped
28th May 2008, 04:25
I solved. It seems the it's enough to call a callback from the connect function:
connect(act_alignright, SIGNAL(triggered(bool)), this, SLOT(right()));
connect(act_alignleft, SIGNAL(triggered(bool)), this, SLOT(left()));
connect(act_aligncenter, SIGNAL(triggered(bool)), this, SLOT(center()));
And then use setAlignment inside the callback.
MaximA
28th May 2008, 18:25
Don't understan:
the slot setAlignement is a slot of textEdit and act_alignright is a signal of a menu.
What am I missing?
G
connect(act_alignright, SIGNAL(triggered(bool)), // <---this is bool
textEdit,SLOT(setAlignment(Qt::AlignRight))); // <--this is not a bool, it is also a value and not a variable
You really need to read/understand: http://doc.trolltech.com/4.4/signalsandslots.html
hope it helps
bugsstring
7th October 2008, 11:08
I have problem with QTextEdit setAlignment()
#ifndef TEDIT_H
#define TEDIT_H
#include <QWidget>
class QToolBar;
class QAction;
class QTextEdit;
class TEdit : public QWidget
{
Q_OBJECT
public:
TEdit();
virtual ~TEdit();
private slots:
void makeBold();
void makeItalic();
void makeUnderline();
void setAlignLeft();
void setAlignCenter();
void setAlignRight();
private:
QToolBar *toolBar;
QAction *textBold;
QAction *textItalic;
QAction *textUnderline;
QAction *textLeft;
QAction *textCenter;
QAction *textRight;
QTextEdit *te;
};
#endif /* TEDIT_H_ */
implement
#include <QtGui>
#include "tedit.h"
TEdit::TEdit()
{
resize(800,600);
te = new QTextEdit;
toolBar = new QToolBar(this);
textBold = new QAction(QIcon(":images/format_text_bold.png"),tr("B"),this);
connect(textBold, SIGNAL(triggered()), this, SLOT(makeBold()));
textItalic = new QAction(QIcon(":images/format_text_italic.png"),tr("I"),this);
connect(textItalic, SIGNAL(triggered()), this, SLOT(makeItalic()));
textUnderline = new QAction(QIcon(":images/format_text_underline.png"),tr("U"),this);
connect(textUnderline, SIGNAL(triggered()), this, SLOT(makeUnderline()));
textLeft = new QAction(QIcon(":images/format_justify_left.png"),tr("Align Left"),this);
connect(textLeft, SIGNAL(triggered()), this, SLOT(setAlignLeft()));
textCenter = new QAction(QIcon(":images/format_justify_center.png"),tr("Align Center"),this);
connect(textLeft, SIGNAL(triggered()), this, SLOT(setAlignCenter()));
textRight = new QAction(QIcon(":images/format_justify_right.png"),tr("Align Right"),this);
connect(textLeft, SIGNAL(triggered()), this, SLOT(setAlignRight()));
toolBar->addAction(textBold);
toolBar->addAction(textItalic);
toolBar->addAction(textUnderline);
toolBar->addSeparator();
toolBar->addAction(textLeft);
toolBar->addAction(textCenter);
toolBar->addAction(textRight);
QVBoxLayout *ml = new QVBoxLayout;
ml->setMargin(1);
ml->setSpacing(0);
ml->addWidget(toolBar);
ml->addWidget(te);
setLayout(ml);
}
void TEdit::makeBold()
{
QTextCursor cursor = te->textCursor();
QTextCharFormat boldFormat;
if(cursor.charFormat().fontWeight()==QFont::Normal )
{
boldFormat.setFontWeight(QFont::Bold);
}
else
{
boldFormat.setFontWeight(QFont::Normal);
}
cursor.mergeCharFormat(boldFormat);
}
void TEdit::makeItalic()
{
QTextCursor cursor = te->textCursor();
QTextCharFormat italicFormat;
if(cursor.charFormat().fontItalic()==false)
{
italicFormat.setFontItalic(true);
}
else
{
italicFormat.setFontItalic(false);
}
cursor.mergeCharFormat(italicFormat);
}
void TEdit::makeUnderline()
{
QTextCursor cursor = te->textCursor();
QTextCharFormat underlineFormat;
if(cursor.charFormat().fontUnderline()==false)
{
underlineFormat.setFontUnderline(true);
}
else
{
underlineFormat.setFontUnderline(false);
}
cursor.mergeCharFormat(underlineFormat);
}
void TEdit::setAlignLeft()
{
QTextCursor cursor = te->textCursor();
QTextBlockFormat blockFmt;
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::BlockUnderCursor);
}
blockFmt.setAlignment(Qt::AlignLeft);
cursor.mergeBlockFormat(blockFmt);
}
void TEdit::setAlignRight()
{
QTextCursor cursor = te->textCursor();
QTextBlockFormat blockFmt;
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::BlockUnderCursor);
}
blockFmt.setAlignment(Qt::AlignRight);
cursor.mergeBlockFormat(blockFmt);
}
void TEdit::setAlignCenter()
{
QTextCursor cursor = te->textCursor();
QTextBlockFormat blockFmt;
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::BlockUnderCursor);
}
blockFmt.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(blockFmt);
}
TEdit::~TEdit()
{
// TODO Auto-generated destructor stub
}
compilation done, no errors or warnings
but if i clicked on toolbar button to align left
text align to right, and alignCenter, alignRight, alignLeft do nothing
O_o
also i try
void TEdit::setAlignLeft()
{
te->setAlignment(Qt::AlignLeft);
}
but not work
bugsstring
7th October 2008, 14:14
sorry problem fixed )))
3 times textLeft )))
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.