PDA

View Full Version : new menus for each line in a qtextedit?



kja
8th December 2010, 23:32
Hi,

I have a text edit with a menu that allows the user to change the color and style of the text block where the cursor is. I want to have my menu have a check next to the current settings (color and style). I have set my actions to checkable but when I first load the program the initial settings are not checked on the menu, they only become checked when I change the settings. How can I have the current setting checked on the menu when I first open the program?

Another problem I'm having is that I want the menus for each block of text in the text edit to be specific to the text block, not the entire text edit. As it is now I only have one qmenu that is for the whole text edit so, for example, if I change some text on the first line to be blue and in italics, and then I put my cursor on the second line and pull up the menu, blue and italics are checked, even though the second line is still black and in Times In New Roman.

Since I want these menus to be specific to each block of text will I have to make a new menu for each line of text?





QMenu *menu = textedit->createStandardContextMenu();
QMenu *subMenu = new QMenu;
subMenu->setTitle("Set Font Style");
QMenu *subMenu2 = new QMenu;
subMenu2->setTitle("Change Color");
menu->addAction(bolder);
menu->addAction(lessBold);

for (int i = 0; i < numbOfStyles; i++){
subMenu->addAction(style[i]);
}
for (int i = 0; i < numbOfColors; i++){
subMenu2->addAction(color[i]);
}
menu->addMenu(subMenu);
menu->addMenu(subMenu2);
menu->exec(textedit->mapToGlobal(pt));
delete menu;
delete subMenu;
delete subMenu2;



Maybe I will have to make an array of menu objects that will corrispond to each block of text?
Perhaps something like this:



QTextCursor cursor = textedit->textCursor();
cursor.movePosition(QTextCursor::Start);

for (int i = 0; i < numberOfTextBlocks; i++){
menu[i] = new QMenu()
}



and then connect a signal every time the user right clicks on the text edit that will connect the cursor area to its corresponding menu?

Am I on the right track, or is there something else I am missing?

Thanks!